方案一:
将下图文件中的 deliverAndDrawFrame 方法
修改为:
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) {
/*----------------------------修改预览旋转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度问题--------------------------------*/
if (mFpsMeter != null) {
mFpsMeter.measure();
mFpsMeter.draw(canvas, 20, 30);
}
getHolder().unlockCanvasAndPost(canvas);
}
}
}
方案二:
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
Mat dst = inputFrame.rgba();
//Log.e(TAG,"orientation : " + this.getRequestedOrientation());
if(current_camera_idx == CameraBridgeViewBase.CAMERA_ID_BACK)
Core.rotate(dst, dst, Core.ROTATE_90_CLOCKWISE);
if(current_camera_idx == CameraBridgeViewBase.CAMERA_ID_FRONT)
{
Core.rotate(dst, dst, Core.ROTATE_90_COUNTERCLOCKWISE);
Core.flip(dst, dst, 1);
}
process_image(dst);
return dst;
}
如上对于后置相机顺时针旋转90度,前置相机逆时针旋转90度,同时做镜像
这么更改后会出现没有图像的问题,logcat会有如下提示:
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;
}
原因是mCacheBitmap
是之前创建的其长宽都和修改后的Mat对象不匹配,导致在Utils.matToBitmap
的时候发生Crash,因此我们需要在转换之前,重新设定bitmap的长宽,如下:
protected void deliverAndDrawFrame(CvCameraViewFrame frame) {
Mat modified;
if (mListener != null) {
modified = mListener.onCameraFrame(frame);
} else {
modified = frame.rgba();
}
+ if(mCacheBitmap != null) {
+ mCacheBitmap.recycle();
+ mCacheBitmap = Bitmap.createBitmap(modified.width(), modified.height(), Bitmap.Config.ARGB_8888);
+ }
boolean bmpValid = true;
if (modified != null) {
更改后图像就能正常的显示了