OpenCV4Android中图像预览旋转90度的问题

在将OpenCV4Android导入到Android项目之后,可以使用JavaCameraView控件调用相机,但是显示预览时,可能出现图像旋转了90度的情况。

在网上找到几种办法,都是修改OpenCV4Android中的CameraBridgeViewBase中的deliverAndDrawFrame方法,分别试了一下:

1、Android开发使用OpenCv中JavaCameraView预览图左旋90度问题

试用了,方法无效,不知道是不是我的其他配置的问题

2、参考了Rotate camera preview to Portrait Android OpenCV Camera的回复

protected void deliverAndDrawFrame(CvCameraViewFrame frame) {
    Mat modified;

    if (mListener != null) {
        modified = mListener.onCameraFrame(frame);
    } else {
        modified = frame.rgba();
    }

    boolean bmpValid = true;
    if (modified != null) {
        try {
            Utils.matToBitmap(modified, mCacheBitmap);
        } catch (Exception e) {
            Log.e(TAG, "Mat type: " + modified);
            Log.e(TAG, "Bitmap type: " + mCacheBitmap.getWidth() + "*" + mCacheBitmap.getHeight());
            Log.e(TAG, "Utils.matToBitmap() throws an exception: " + e.getMessage());
            bmpValid = false;
        }
    }

    if (bmpValid && mCacheBitmap != null) {
        Canvas canvas = getHolder().lockCanvas();
        if (canvas != null) {
            canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
            // 修改预览旋转90问题
            // 能够通过旋转,正常显示预览,但是图像被缩放了
            //canvas.drawBitmap(mCacheBitmap, (canvas.getWidth() - mCacheBitmap.getWidth()) / 2, (canvas.getHeight() - mCacheBitmap.getHeight()) / 2, null);
            //Change to support portrait view
            Matrix matrix = new Matrix();
            matrix.preTranslate((canvas.getWidth() - mCacheBitmap.getWidth()) / 2, (canvas.getHeight() - mCacheBitmap.getHeight()) / 2);

            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
                matrix.postRotate(90f, (canvas.getWidth()) / 2, (canvas.getHeight()) / 2);
            canvas.drawBitmap(mCacheBitmap, matrix, new Paint());
            // 修改预览旋转90问题end

            if (mFpsMeter != null) {
                mFpsMeter.measure();
                mFpsMeter.draw(canvas, 20, 30);
            }
            getHolder().unlockCanvasAndPost(canvas);
        }
    }
}

修改后的效果就是图像没有全屏。

3、还是参考上面的回答

protected void deliverAndDrawFrame(CvCameraViewFrame frame) {
    Mat modified;

    if (mListener != null) {
        modified = mListener.onCameraFrame(frame);
    } else {
        modified = frame.rgba();
    }

    boolean bmpValid = true;
    if (modified != null) {
        try {
            Utils.matToBitmap(modified, mCacheBitmap);
        } catch (Exception e) {
            Log.e(TAG, "Mat type: " + modified);
            Log.e(TAG, "Bitmap type: " + mCacheBitmap.getWidth() + "*" + mCacheBitmap.getHeight());
            Log.e(TAG, "Utils.matToBitmap() throws an exception: " + e.getMessage());
            bmpValid = false;
        }
    }

    if (bmpValid && mCacheBitmap != null) {
        Canvas canvas = getHolder().lockCanvas();
        if (canvas != null) {
            canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);

            // 修改预览旋转90度问题
            canvas.rotate(90,0,0);
            float scale = canvas.getWidth() / (float)mCacheBitmap.getHeight();
            float scale2 = canvas.getHeight() / (float)mCacheBitmap.getWidth();
            if(scale2 > scale){
                scale = scale2;
            }
            if (scale != 0) {
                canvas.scale(scale, scale,0,0);
            }
            canvas.drawBitmap(mCacheBitmap, 0, -mCacheBitmap.getHeight(), null);
            // 修改预览旋转90度问题end

            if (mFpsMeter != null) {
                mFpsMeter.measure();
                mFpsMeter.draw(canvas, 20, 30);
            }
            getHolder().unlockCanvasAndPost(canvas);
        }
    }
}

这样预览图片可以全屏,并且是正的了。



你可能感兴趣的:(Android,OpenCV,图像处理)