相机像素旋转

Enr*_*que 12 android image-processing android-camera

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

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

我得到方法内的像素 onPreviewFrame(byte[] data, Camera camera)

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

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

我也尝试过:

parameters.setRotation(90);
camera.setParameters(parameters);
Run Code Online (Sandbox Code Playgroud)

但那没用.

我正在使用android 2.2

在此输入图像描述

顶部图像显示使用时的SurfaceView camera.setDisplayOrientation(90);

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

小智 7

虽然我有点迟到了,但这是我写的一种方法,其他人可能会发现旋转YUV420sp(NV21)图像很有用.我在SO上看到的所有其他方法似乎都没有.

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;
}
Run Code Online (Sandbox Code Playgroud)


Mat*_*tag 5

也许你可以在侧面框架上执行图像处理,并且只在显示它时担心旋转(你已经完成了).如果没有,您需要以旧式方式旋转框架.我在某处找到了一些代码(稍作修改)可能会有所帮助:

// load the origial bitmap
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
       R.drawable.android);

int width = bitmap.width();
int height = bitmap.height();

// create a matrix for the manipulation
Matrix matrix = new Matrix();
// rotate the Bitmap 90 degrees (counterclockwise)
matrix.postRotate(90);

// recreate the new Bitmap, swap width and height and apply transform
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                  width, height, matrix, true);
Run Code Online (Sandbox Code Playgroud)

这很容易,因为createBitmap方法支持矩阵变换:http://developer.android.com/reference/android/graphics/Bitmap.html#createBitmap( android.graphics.Bitmap ,int,int,int,int,android. graphics.Matrix,boolean)