在Android中使用OpenCV将NV21转换为RGB

我想在Android中使用OpenCV。 所以我首先通过并排放置两个SurfaceView来测试OpenCV。 一个SurfaceView用于从相机预览输出(输出格式显然是NV21)。 另一个SurfaceView在通过OpenCV后显示相同的预览,如下面的代码所示:

public void onPreviewFrame(byte[] data, Camera camera) { // TODO Auto-generated method stub if( mYuv != null ) mYuv.release(); mYuv = new Mat( height + height/2, width, CvType.CV_8UC1 ); mYuv.put( 0, 0, data); Mat mRgba = new Mat(); Imgproc.cvtColor( mYuv, mRgba, Imgproc.COLOR_YUV2RGB_NV21, 4 ); Bitmap map = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888 ); Utils.matToBitmap( mRgba, map ); preview.setBackgroundDrawable( new BitmapDrawable( map )); mRgba.release(); } 

但是通过OpenCV后得到的图像是一个绿色的,有条件的……事情:

绿色,有条不紊的东西

有任何想法吗?

编辑:

根据评论修改了一点代码。

 public void onPreviewFrame(byte[] data, Camera camera) { // TODO Auto-generated method stub if( mYuv != null ) mYuv.release(); mYuv = new Mat( height + height/2, width, CvType.CV_8UC1 ); mYuv.put( 0, 0, data ); Mat mRgba = new Mat(); Imgproc.cvtColor( mYuv, mRgba, Imgproc.COLOR_YUV2RGBA_NV21, 4 ); Bitmap map = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888 ); Utils.matToBitmap( mRgba, map ); preview.setBackgroundDrawable( new BitmapDrawable( where.getResources(), map )); mRgba.release(); } 

结果如下: 再次丑陋的绿色事物。

好吧,我弄清楚我在哪里乱了。

我最初做了这样的事情:

 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { this.width = width; this.height = height; params.setPreviewSize( width, height ); camera.setParameters( params ); camera.startPreview(); } 

问题是,android上的摄像头只支持特定的预览分辨率。 因此我设置的具体分辨率不起作用。 所以,我改成了这个:

 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Size size = params.getPreviewSize(); this.height = size.height; this.width = size.width; camera.setParameters( params ); camera.startPreview(); } 

然后一切正常! 老实说,这不是我预期的错误,所以这不是一个很好的问题。