Rotate camera preview to Portrait Android OpenCV Camera

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

    Another solution. i think this better

    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);
    
                int currentOrientation = getResources().getConfiguration().orientation;
                if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
                    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);
                    }
                } else {
    
                    if (mScale != 0) {
                        Bitmap bitmap = Bitmap.createScaledBitmap(mCacheBitmap, canvas.getHeight(), canvas.getWidth(), true);
                        canvas.drawBitmap(bitmap, new Rect(0,0,bitmap.getWidth(), bitmap.getHeight()), new Rect(
                                (int)((canvas.getWidth() - mScale*bitmap.getWidth()) / 2),
                                (int)(0),
                                (int)((canvas.getWidth() - mScale*bitmap.getWidth()) / 2 + mScale*bitmap.getWidth()),
                                (int)((canvas.getHeight()))), null);
                    } else {
                        Bitmap bitmap = Bitmap.createScaledBitmap(mCacheBitmap, canvas.getHeight(), canvas.getWidth(), true);
                        canvas.drawBitmap(bitmap, new Rect(0,0,bitmap.getWidth(), bitmap.getHeight()), new Rect(
                                (int)((canvas.getWidth() - bitmap.getWidth()) / 2),
                                (int)(0),
                                (int)((canvas.getWidth() - bitmap.getWidth()) / 2 + bitmap.getWidth()),
                                (int)(canvas.getHeight())), null);
                    }
                }
    
                if (mFpsMeter != null) {
                    mFpsMeter.measure();
                    mFpsMeter.draw(canvas, 20, 30);
                }
                getHolder().unlockCanvasAndPost(canvas);
            }
        }
    }
    

    and...

    @Override
    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        MatOfRect faces = new MatOfRect();
        int currentOrientation = getResources().getConfiguration().orientation;
        if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) { 
            mRgba = inputFrame.rgba();
            mGray = inputFrame.gray();
    
            int height = mGray.rows();
            if (Math.round(height * 0.2) > 0) {
                mFaceSize = (int) Math.round(height * 0.2);
            }
    
            cascadeClassifier.detectMultiScale(mGray, faces, 1.1, 3, 2,
                    new Size(mFaceSize, mFaceSize));
            Rect[] facesArray = faces.toArray();
            for (int i = 0; i < facesArray.length; i++) {
                Point center = new Point(facesArray[i].x + facesArray[i].width / 2,
                        facesArray[i].y + facesArray[i].height / 2);
                rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(), FACE_RECT_COLOR, 3);
            }
    
        } else {
            mRgba = inputFrame.rgba();
            mGray = inputFrame.gray();
    
            Mat rotImage = Imgproc.getRotationMatrix2D(new Point(mRgba.cols() / 2,
                    mRgba.rows() / 2), 90, 1.0);
    
            Imgproc.warpAffine(mRgba, mRgba, rotImage, mRgba.size());
            Imgproc.warpAffine(mGray, mGray, rotImage, mRgba.size());
    
            Core.flip(mRgba, mRgba, 1);
            Core.flip(mGray, mGray, 1);
    
            int height = mGray.rows();
            if (Math.round(height * 0.2) > 0) {
                mFaceSize = (int) Math.round(height * 0.2);
            }
    
            cascadeClassifier.detectMultiScale(mGray, faces, 1.1, 3, 2,
                    new Size(mFaceSize, mFaceSize));
            Rect[] facesArray = faces.toArray();
            for (int i = 0; i < facesArray.length; i++) {
                Point center = new Point(facesArray[i].x + facesArray[i].width / 2,
                        facesArray[i].y + facesArray[i].height / 2);
                rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(), FACE_RECT_COLOR, 3);
            }
        }
    
        return mRgba;
    

提交回复
热议问题