且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

旋转相机预览到人像 Android OpenCV 相机

更新时间:2022-02-04 04:34:55

我在尝试实现 OpenCV 时遇到了同样的问题.我能够通过对 deliveryAndDrawFrame 方法进行以下更改来修复它.

I had the same problem trying to implement OpenCV. I was able to fix it by making the following changes to the deliverAndDrawFrame method.

  1. 旋转画布对象

  1. Rotate the canvas object

Canvas canvas = getHolder().lockCanvas();
// Rotate canvas to 90 degrees
canvas.rotate(90f, canvas.getWidth()/2, canvas.getHeight()/2);

  • 在绘制之前调整位图的大小以适合整个画布大小

  • Resize the bitmap to fit entire size of canvas before drawing

    // Resize
    Bitmap bitmap = Bitmap.createScaledBitmap(mCacheBitmap, canvas.getHeight(), canvas.getWidth(), true);
    // Use bitmap instead of mCacheBitmap
    canvas.drawBitmap(bitmap, new Rect(0,0,bitmap.getWidth(), bitmap.getHeight()), new Rect(
        (int)((canvas.getWidth() - mScale*bitmap.getWidth()) / 2),
        (int)((canvas.getHeight() - mScale*bitmap.getHeight()) / 2),
        (int)((canvas.getWidth() - mScale*bitmap.getWidth()) / 2 + mScale*bitmap.getWidth()),
        (int)((canvas.getHeight() - mScale*bitmap.getHeight()) / 2 + mScale*bitmap.getHeight()
      )), null);
    
    // Unlock canvas
    getHolder().unlockCanvasAndPost(canvas);