4 channel IplImage javacv to android bitmap

旧街凉风 提交于 2019-12-13 04:06:10

问题


I'm trying to record video by checking each frame of camera preview to bitmap with quality ARGB_8888. As it required 4 channel, Created IplImage with channel 4 too. Now the output have two major problems :

1) Bitmap that created from IplImage have grayscale. even if I have converted it from BGR2RGBA. 2) 4 channel IplImage gave me bitmap (divided in 4 parts) with same screen.

Let me put my code over here.

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {

        if (yuvIplimage != null && recording) {
            videoTimestamp = 1000 * (System.currentTimeMillis() - startTime);


            // Where imagewidth = 640 and imageheight  = 480 (As per camera preview size)
            // Create the yuvIplimage 

            IplImage yuvimage = IplImage.create(imageWidth, imageHeight * 3 / 2, IPL_DEPTH_8U, 2);
            yuvimage.getByteBuffer().put(data);


            IplImage rgbimage = IplImage.create(imageWidth, imageHeight, IPL_DEPTH_8U, 3);
            opencv_imgproc.cvCvtColor(yuvimage, rgbimage, opencv_imgproc.CV_YUV2BGR_NV21);

            Bitmap bitmap = Bitmap.createBitmap(imageWidth, imageHeight,Bitmap.Config.RGB_565);
            bitmap.copyPixelsFromBuffer(rgbimage.getByteBuffer());

            //Save file to SDCARD------------

            File file = new File(Environment.getExternalStorageDirectory(),
                    "rgbbitmap.png");
            FileOutputStream fOut;
            try {
                fOut = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                fOut.flush();
                fOut.close();
                // mybitmap.recycle();

            } catch (Exception e) { // TODO

            }       


            try {

                // Get the correct time
                recorder.setTimestamp(videoTimestamp);

                // Record the image into FFmpegFrameRecorder
                recorder.record(yuvimage);

            } catch (FFmpegFrameRecorder.Exception e) {
                Log.v(LOG_TAG, e.getMessage());
                e.printStackTrace();
            }
        }
    }

As well as, find the below bitmap image as I'm getting as output with 4 parts of same frame.

What's wrong with my code or what's missing by me? Let me know your best suggestions.

Thanks,


回答1:


IplImage yuvImage = IplImage.create(width, height * 3 / 2, IPL_DEPTH_8U, 1);
yuvImage.getByteBuffer().put(data);
IplImage bgrImage = IplImage.create(width, height, IPL_DEPTH_8U, 3);
cvCvtColor(yuvImage, bgrImage, CV_YUV2BGR_NV21);
cvSaveImage("/mnt/sdcard/result.jpg", bgrImage);


来源:https://stackoverflow.com/questions/23149867/4-channel-iplimage-javacv-to-android-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!