public void onPreviewFrame(byte[] data, Camera camera) {}方法获取到的数据为YUV420格式数据
/******************顺时针旋转90度,注意,旋转后宽高是调换了的哦******************/
public static byte[] rotateYUV420Degree90(byte[] data,int imageWidth,
int imageHeight) {
byte[] yuv =new byte[imageWidth * imageHeight *3 /2];
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++;
}
}
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;
}
/******************顺时针旋转180度,注意,宽高不调换******************/
private static byte[] rotateYUV420Degree180(byte[] data,int imageWidth,int imageHeight) {
byte[] yuv =new byte[imageWidth * imageHeight *3 /2];
int i =0;
int count =0;
for (i = imageWidth * imageHeight -1; i >=0; i--) {
yuv[count] = data[i];
count++;
}
i = imageWidth * imageHeight *3 /2 -1;
for (i = imageWidth * imageHeight *3 /2 -1; i >= imageWidth
* imageHeight; i -=2) {
yuv[count++] = data[i -1];
yuv[count++] = data[i];
}
return yuv;
}
/******************顺时针旋转270度,注意,旋转后宽高是调换了的哦******************/
public static byte[] YUV420spRotate270(byte[] src,int width,int height) {
int count =0;
int uvHeight = height >>1;
int imgSize = width * height;
byte[] des =new byte[imgSize *3 >>1];//copy y
for (int j = width -1; j >=0; j--) {
for (int i =0; i < height; i++) {
des[count++] = src[width * i + j];
}
}//u,v
for (int j = width -1; j >0; j -=2) {
for (int i =0; i < uvHeight; i++) {
des[count++] = src[imgSize + width * i + j -1];
des[count++] = src[imgSize + width * i + j];
}
}
return des;
}
/******************顺时针旋转90度,注意,旋转后宽高是调换了的哦******************/
data = rotateYUV420Degree90(data, previewSize.width, previewSize.height);
/*****************将data数组转换为bitmap******************/
BitmapFactory.Options newOpts = new BitmapFactory.Options();
newOpts.inJustDecodeBounds = true;
YuvImage yuvimage = new YuvImage(
data,
ImageFormat.NV21,
previewSize.width,
previewSize.height,
null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yuvimage.compressToJpeg(new Rect(0, 0, previewSize.width, previewSize.height), 100, baos);// 80--JPG图片的质量[0-100],100最高byte[] rawImage = baos.toByteArray();
//将rawImage转换成bitmap
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bitmap = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length, options);
if (bitmap != null) {
if (iv != null)
iv.setImageBitmap(bitmap);
}