相机像素旋转 [英] camera pixels rotated

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

问题描述

我想对从相机获取的像素进行一些图像处理.

I want to do some image processing to the pixels gotten from the camera.

问题是来自相机的像素旋转了 90 度.

The problem is that the pixels from the camera are rotated 90 degrees.

我正在获取方法中的像素 onPreviewFrame(byte[] data, Camera camera)

Im getting the pixels inside the method onPreviewFrame(byte[] data, Camera camera)

我尝试了 camera.setDisplayOrientation(90); 并且它以正确的方向显示视频,但我仍然收到文档中所述的旋转像素:

I tried camera.setDisplayOrientation(90); and it displays the video in the correct orientation but I am still receiving the rotated pixels as stated in the documentation:

这不影响传入的字节数组的顺序Android.Hardware.Camera.IPreviewCallback.OnPreviewFrame(Byte[],Android.Hardware.Camera)、JPEG 图片或录制的视频.

This does not affect the order of byte array passed in Android.Hardware.Camera.IPreviewCallback.OnPreviewFrame(Byte[], Android.Hardware.Camera), JPEG pictures, or recorded videos.

我也试过:

parameters.setRotation(90);
camera.setParameters(parameters);

但这没有用.

我使用的是安卓 2.2

I'm using android 2.2

上图显示了使用camera.setDisplayOrientation(90);

第二个图像是从 data 数组中的 onPreviewFrame(byte[] data, Camera camera) 中获取的.如您所见,data 数组旋转了.

The second image is gotten inside onPreviewFrame(byte[] data, Camera camera) from the data array. As you can see the data array comes rotated.

推荐答案

虽然我参加聚会有点晚了,但我写了一个方法,其他人可能会发现它对旋转 YUV420sp (NV21) 图像很有用.我在 SO 上看到的其他方法似乎都没有真正做到这一点.

While I'm a bit late to the party, here's a method I wrote that others might find useful to rotate a YUV420sp (NV21) image. None of the other methods I saw on SO seemed to actually do it.

public static byte[] rotateNV21(final byte[] yuv,
                                final int width,
                                final int height,
                                final int rotation)
{
  if (rotation == 0) return yuv;
  if (rotation % 90 != 0 || rotation < 0 || rotation > 270) {
    throw new IllegalArgumentException("0 <= rotation < 360, rotation % 90 == 0");
  }

  final byte[]  output    = new byte[yuv.length];
  final int     frameSize = width * height;
  final boolean swap      = rotation % 180 != 0;
  final boolean xflip     = rotation % 270 != 0;
  final boolean yflip     = rotation >= 180;

  for (int j = 0; j < height; j++) {
    for (int i = 0; i < width; i++) {
      final int yIn = j * width + i;
      final int uIn = frameSize + (j >> 1) * width + (i & ~1);
      final int vIn = uIn       + 1;

      final int wOut     = swap  ? height              : width;
      final int hOut     = swap  ? width               : height;
      final int iSwapped = swap  ? j                   : i;
      final int jSwapped = swap  ? i                   : j;
      final int iOut     = xflip ? wOut - iSwapped - 1 : iSwapped;
      final int jOut     = yflip ? hOut - jSwapped - 1 : jSwapped;

      final int yOut = jOut * wOut + iOut;
      final int uOut = frameSize + (jOut >> 1) * wOut + (iOut & ~1);
      final int vOut = uOut + 1;

      output[yOut] = (byte)(0xff & yuv[yIn]);
      output[uOut] = (byte)(0xff & yuv[uIn]);
      output[vOut] = (byte)(0xff & yuv[vIn]);
    }
  }
  return output;
}

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

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