旋转相机预览到人像 Android OpenCV 相机 [英] Rotate camera preview to Portrait Android OpenCV Camera

查看:32
本文介绍了旋转相机预览到人像 Android OpenCV 相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 OpenCV 2.4.3.2 创建相机应用程序并进行一些 opencv 处理.我希望它能够有多个 UI 方向,而不仅仅是横向.

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.

问题是当我将方向更改为纵向时,图像会横向出现.

The problem is that when I change the orientation to portrait, the image comes out sideways.

我知道我可以在做图像之前旋转输入图像处理(因此仅将方向保留为横向),这很好并且有效,但不能解决我的 UI 的其余部分将处于错误方向的问题.

I understand that I could just rotate the input image before doing image processing (and thus leave the orientation as landscape only), which is fine and works, but doesn't solve the problem that the rest of my UI will be in the wrong orientation.

我也尝试使用 此代码将相机旋转 90 度,但它只是似乎不起作用.

I have also tried using this code to rotate the camera 90deg, but it just doesn't seem to work.

mCamera.setDisplayOrientation(90);

它要么没有效果,要么有时只是导致预览变黑

It either has no effect, or sometimes just causes the preview to be blacked out

有没有人用 OpenCV 成功地做到了这一点?我的课程从 JavaCameraView 扩展而来.

Has anyone done this successfully with OpenCV? My class extends from JavaCameraView.

我做了一个改进,即我在 OpenCV 内部旋转了图像,因为它显示在 CameraBridgeViewBase.java 类中.

I have made an improvement, which is that I have rotated the image inside of OpenCV as it is displayed in the CameraBridgeViewBase.java class.

在交货和并条法中:

if (canvas != null) {
            canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
            //canvas.drawBitmap(mCacheBitmap, (canvas.getWidth() - mCacheBitmap.getWidth()) / 2, (canvas.getHeight() - mCacheBitmap.getHeight()) / 2, null);
            //Change to support portrait view
            Matrix matrix = new Matrix();
            matrix.preTranslate((canvas.getWidth() - mCacheBitmap.getWidth()) / 2,(canvas.getHeight() - mCacheBitmap.getHeight()) / 2);

            if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
                matrix.postRotate(90f,(canvas.getWidth()) / 2,(canvas.getHeight()) / 2);
            canvas.drawBitmap(mCacheBitmap, matrix, new Paint());

...基本上,这只是像这样旋转输入图像

... Basically, this just roatates the input image like so

这更好,但我显然希望它是全屏的.

This is better, but I obviously want this to be full screen.

推荐答案

我在尝试实现 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);
    

  • 这篇关于旋转相机预览到人像 Android OpenCV 相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆