本文主要是介绍OpenCV Android 摄像头满屏和翻转问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
修改:
CameraBridgeViewBase.java类的deliverAndDrawFrame(CvCameraViewFrame frame)方法:
/*** This method shall be called by the subclasses when they have valid* object and want it to be delivered to external client (via callback) and* then displayed on the screen.* @param frame - the current frame to be delivered*/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);if (BuildConfig.DEBUG)Log.d(TAG, "mStretch value: " + mScale);/*----------------------------注释原有代码 start--------------------------------*/
// if (mScale != 0) {
// canvas.drawBitmap(mCacheBitmap, new Rect(0,0,mCacheBitmap.getWidth(), mCacheBitmap.getHeight()),
// new Rect((int)((canvas.getWidth() - mScale*mCacheBitmap.getWidth()) / 2),
// (int)((canvas.getHeight() - mScale*mCacheBitmap.getHeight()) / 2),
// (int)((canvas.getWidth() - mScale*mCacheBitmap.getWidth()) / 2 + mScale*mCacheBitmap.getWidth()),
// (int)((canvas.getHeight() - mScale*mCacheBitmap.getHeight()) / 2 + mScale*mCacheBitmap.getHeight())), null);
// } else {
// canvas.drawBitmap(mCacheBitmap, new Rect(0,0,mCacheBitmap.getWidth(), mCacheBitmap.getHeight()),
// new Rect((canvas.getWidth() - mCacheBitmap.getWidth()) / 2,
// (canvas.getHeight() - mCacheBitmap.getHeight()) / 2,
// (canvas.getWidth() - mCacheBitmap.getWidth()) / 2 + mCacheBitmap.getWidth(),
// (canvas.getHeight() - mCacheBitmap.getHeight()) / 2 + mCacheBitmap.getHeight()), null);
// }/*----------------------------注释原有代码 end--------------------------------*//*----------------------------修改满屏问题--------------------------------*/canvas.drawBitmap(mCacheBitmap, new Rect(0,0,mCacheBitmap.getWidth(), mCacheBitmap.getHeight()),new Rect(0,0,canvas.getWidth(),canvas.getHeight()), 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);}}}
这篇关于OpenCV Android 摄像头满屏和翻转问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!