Rotate camera preview to Portrait Android OpenCV Camera

前端 未结 17 1598
日久生厌
日久生厌 2020-12-07 15:45

I am trying to use OpenCV 2.4.3.2 to create a camera app and do some opencv processing. I would like it to be able to have multiple UI orientations, not just Landscape.

17条回答
  •  孤城傲影
    2020-12-07 16:14

    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. Rotate the canvas object

      Canvas canvas = getHolder().lockCanvas();
      // Rotate canvas to 90 degrees
      canvas.rotate(90f, canvas.getWidth()/2, canvas.getHeight()/2);
      
    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);
      

提交回复
热议问题