dji-sdk / Mobile-SDK-Android-V5

MSDK V5 Sample
Other
266 stars 140 forks source link

Capture frame with CameraStreamManager #404

Closed IakovlevAA closed 4 weeks ago

IakovlevAA commented 1 month ago

Hi DJI, I found in sample app possibilty of catching frames from camera stream. Is it possible, that I can capture frames and turning them into .png pictures? This is code from Sample

 MediaDataCenter.getInstance().cameraStreamManager.addFrameListener(
            cameraIndex,
            format,
            object : ICameraStreamManager.CameraFrameListener {
                override fun onFrame(frameData: ByteArray, offset: Int, length: Int, width: Int, height: Int, format: FrameFormat) {
                    try {
                        val dirs = File(DiskUtil.getExternalCacheDirPath(ContextUtil.getContext(), "CameraStreamImageDir"))
                        if (!dirs.exists()) {
                            dirs.mkdirs()
                        }
                        val file = File(dirs.absolutePath, "$formatName.image")
                        FileOutputStream(file).use { stream ->
                            stream.write(frameData, offset, length)
                            stream.flush()
                            stream.close()
                            ToastUtils.showToast("Save to : ${file.path}")
                        }
                        LogUtils.i(TAG, "Save to : ${file.path}")
                    } catch (e: Exception) {
                        ToastUtils.showToast("save fail$e")
                    }
                    // Because only one frame needs to be saved, you need to call removeOnFrameListener here
                    // If you need to read frame data for a long time, you can choose to actually call remove OnFrameListener according to your needs
                    MediaDataCenter.getInstance().cameraStreamManager.removeFrameListener(this)
                }

            })

Does .image format can be changed to another(.yuv) ? If I save frame as .yuv and convert to .png I can't get normal picture

dji-dev commented 1 month ago

Agent comment from yating.liao in Zendesk ticket #115208:

The LiveStream does not have an interface to obtain frame data. The image pushed by LiveStream is the camera's image; can we use the camera's frame data as a substitute?

°°°

IakovlevAA commented 1 month ago

Yes, I meant obtaining frames from camera and converting them to Bitmap

dji-dev commented 4 weeks ago

Agent comment from yating.liao in Zendesk ticket #115208:

You may want to take a look at this issue. If you obtain the YUV data, you can implement the conversion to bitmap on your own. https://sdk-forum.dji.net/hc/en-us/community/posts/33709871608089-YUV-to-Bitmap

°°°

IakovlevAA commented 4 weeks ago

Yes, thanks. This approach helped me, for anyone who will wonder I'll leave sample of code that works for me

val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val byteBuffer = ByteBuffer.wrap(frameData)
bitmap.copyPixelsFromBuffer(byteBuffer)
val stream = ByteArrayOutputStream()
bitmap?.compress(Bitmap.CompressFormat.JPEG, 100, stream)
val image = stream.toByteArray()
dji-dev commented 4 weeks ago

Agent comment from yating.liao in Zendesk ticket #115208:

I am pleased to help you.

°°°