本文介绍了Android:快速旋转CameraPreview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenCv构建人脸检测应用程序.我正在处理在回调onPreviewFrame上收到的预览帧数据.我在纵向模式下使用相机,而onPreviewFrame在横向模式下返回了我的数据.我正在使用此代码旋转帧数据.

I am building Face detection app using OpenCv. I am processing preview frame data received on callback onPreviewFrame. I am using camera in portrait mode, whereas onPreviewFrame returns me data in landscape mode. I am rotating frame data using this code.

public static byte[] rotateYUV420Degree90(byte[] data, int imageWidth, int imageHeight) {
    byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2];
    // Rotate the Y luma
    int i = 0;
    for (int x = 0; x < imageWidth; x++) {
        for (int y = imageHeight - 1; y >= 0; y--) {
            yuv[i] = data[y * imageWidth + x];
            i++;
        }
    }
    // Rotate the U and V color components
    i = imageWidth * imageHeight * 3 / 2 - 1;
    for (int x = imageWidth - 1; x > 0; x = x - 2) {
        for (int y = 0; y < imageHeight / 2; y++) {
            yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + x];
            i--;
            yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + (x - 1)];
            i--;
        }
    }
    return yuv;
}

旋转数据后,我正在将字节数组转换为OpenCv Mat.转换后,我进入了openCv本机代码.

After rotating data, I am converting to byte array to OpenCv Mat. After the conversion I pass into openCv native code.

在横向模式下(不旋转预览数据),处理相机预览后,我可以获得大约20 FPS.但是在纵向模式下,通过上述方法,FPS降低为3 FPS.在测量rotateYUV420Degree90花费的时间时,此方法是主要的罪魁祸首.

In landscape mode (without rotating preview data), I am able to get almost 20 FPS after processing camera preview. But in portrait mode, with above method, the FPS is reduced to 3 FPS. On measuring time taken by rotateYUV420Degree90, this method is the main culprit.

我是OpenCv的新手.我还有其他方法可以快速使用Java代码或本机代码旋转预览数据.由于我的应用程序很复杂,因此我无法使用OpenCV提供的JavaCameraView.

I am new to OpenCv. Is there any other approach that i can take to rotate preview data using java code or native code, fastly. Because of complexity of my app, i cannot use JavaCameraView provided by OpenCV.

推荐答案

问题:不要以列顺序遍历图像.由于您不会从引用的位置中受益,因此代码将运行得非常慢.

Problem: Dont traverse image in column-order. Since, you are not benefited by locality of reference, the code will run very slow.

对于opencv,您可以结合使用转置和翻转命令.

For opencv, you can use combination of transpose and flip command.

switch (angle) {
        case 0:
            srcImage.copyTo(dstImage);
            break;
        case 90:
            Core.transpose(srcImage, dstImage);
            Core.flip(dstImage, dstImage, 1);
            break;
        case 180:
            Core.flip(srcImage, dstImage, -1);
            break;
        case 270:
            Core.transpose(srcImage, dstImage);
            Core.flip(dstImage, dstImage, 0);
            break;
        default:
            Logger.error(
                "ROTATE_IMAGE_CLOCKWISE: Incorrect rotation value received: {}", 
                angle);
            return srcImage;
    }

当Image(jpeg)具有方向元数据时,这与用于读取输入图像的代码相同.请参见opencv源代码此处.上面的代码在Java中.

This is the same code used to read input image when Image(jpeg) has Orientation metadata. See opencv source code here. The above code is in java.

此处中查看我的答案,以了解定向的工作原理.将我的答案中的opencv和图像放在一起,非常简单.

See my answer here, to know how orientation works. Put opencv and the image from my answer together, and its very simple.

这篇关于Android:快速旋转CameraPreview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 12:17