Rotate camera preview to Portrait Android OpenCV Camera

前端 未结 17 1535
日久生厌
日久生厌 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:27

    I have the same issue, i have had figure out it!! and there is my solution:

    as the part of first, In CameraBridgeViewBase.Java, the two constructor, add initialization of WindowManager:

    public CameraBridgeViewBase(Context context, int cameraId) {  
       super(context);  
       mCameraIndex = cameraId;  
       getHolder().addCallback(this);  
       mMaxWidth = MAX_UNSPECIFIED;  
       mMaxHeight = MAX_UNSPECIFIED;  
       windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);  
    }  
    
    
    public CameraBridgeViewBase(Context context, AttributeSet attrs) {  
       super(context, attrs);  
       int count = attrs.getAttributeCount();  
       Log.d(TAG, "Attr count: " + Integer.valueOf(count));  
    
       TypedArray styledAttrs = getContext().obtainStyledAttributes(attrs, R.styleable.CameraBridgeViewBase);  
       if (styledAttrs.getBoolean(R.styleable.CameraBridgeViewBase_show_fps, false))  
           enableFpsMeter();  
    
       mCameraIndex = styledAttrs.getInt(R.styleable.CameraBridgeViewBase_camera_id, -1);  
    
       getHolder().addCallback(this);  
       mMaxWidth = MAX_UNSPECIFIED;  
       mMaxHeight = MAX_UNSPECIFIED;  
       windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);  
       styledAttrs.recycle();  
    }  
    

    then ,You need to replace function deliverAndDrawFrame(CvCameraViewFrame frame) as follows ,

    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);  
              int rotation = windowManager.getDefaultDisplay().getRotation();  
              int degrees = 0;  
              // config degrees as you need  
              switch (rotation) {  
                  case Surface.ROTATION_0:  
                      degrees = 90;  
                      break;  
                  case Surface.ROTATION_90:  
                      degrees = 0;  
                      break;  
                  case Surface.ROTATION_180:  
                      degrees = 270;  
                      break;  
                  case Surface.ROTATION_270:  
                      degrees = 180;  
                      break;  
              }  
    
              Matrix matrix = new Matrix();  
              matrix.postRotate(degrees);  
              Bitmap outputBitmap = Bitmap.createBitmap(mCacheBitmap, 0, 0, mCacheBitmap.getWidth(), mCacheBitmap.getHeight(), matrix, true);  
    
              if (outputBitmap.getWidth() <= canvas.getWidth()) {  
                  mScale = getRatio(outputBitmap.getWidth(), outputBitmap.getHeight(), canvas.getWidth(), canvas.getHeight());  
              } else {  
                  mScale = getRatio(canvas.getWidth(), canvas.getHeight(), outputBitmap.getWidth(), outputBitmap.getHeight());  
              }  
    
              if (mScale != 0) {  
                  canvas.scale(mScale, mScale, 0, 0);  
              }  
              Log.d(TAG, "mStretch value: " + mScale);  
    
              canvas.drawBitmap(outputBitmap, 0, 0, null);  
    
              if (mFpsMeter != null) {  
                  mFpsMeter.measure();  
                  mFpsMeter.draw(canvas, 20, 30);  
              }  
              getHolder().unlockCanvasAndPost(canvas);
          }  
       }  
    }  
    

    and add this function extra,

    private float getRatio(int widthSource, int heightSource, int widthTarget, int heightTarget) {  
       if (widthTarget <= heightTarget) {  
           return (float) heightTarget / (float) heightSource;  
       } else {  
           return (float) widthTarget / (float) widthSource;  
       }  
    }  
    

    it's okey ,and If this answer useful to you, please mark 'accepted' Help Reputation

提交回复
热议问题