AndroidOpenCV摄像头预览旋转90度问题

本文介绍了一种修改Android应用程序中相机预览的方法,通过调整图像旋转角度并自适应缩放,确保了显示效果的准确性和视觉体验的一致性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将下图文件中的  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(Exceptione) {
           
Log.e(TAG, "Mattype: " + modified);
           
Log.e(TAG, "Bitmaptype: " + 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, "mStretchvalue: " + mScale);

            if (mScale != 0) {
               canvas.drawBitmap(mCacheBitmap, new Rect(0,0,mCacheBitmap.getWidth(),mCacheBitmap.getHeight()),
                     newRect((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()),
                     newRect((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);
            }*/

            /*----------------------------修改预览旋转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);
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值