import android.content.Context;
import android.graphics.Bitmap;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicYuvToRGB;
import android.renderscript.Type;
/**
* 使用RenderScript将视频YUV流转换为BMP
* 注:这个类适用于CameraPreview不变的情况
*/
public class FastYUVtoRGB {
private RenderScript rs;
private ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic;
private Type.Builder yuvType, rgbaType;
private Allocation in, out;
public FastYUVtoRGB(Context context) {
rs = RenderScript.create(context);
yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
}
public Bitmap convertYUVtoRGB(byte[] yuvData, int width, int height) {
if (yuvType == null) {
yuvType = new Type.Builder(rs, Element.U8(rs)).setX(yuvData.length);
in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height);
out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);
}
in.copyFrom(yuvData);
yuvToRgbIntrinsic.setInput(in);
yuvToRgbIntrinsic.forEach(out);
Bitmap bmpout = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
out.copyTo(bmpout);
return bmpout;
}
}
当有了bitmap之后,如果使用Bitmap.createBitmap()方法来进行图片变换,速度较慢,推荐使用canvas来变换bitmap
首先需要构建正确的变换矩阵(代码来自于tensorflow官方git)
更新:加入水平翻转与垂直翻转参数
/**
* Returns a transformation matrix from one reference frame into another.
* Handles cropping (if maintaining aspect ratio is desired) and rotation.
*
* @param srcWidth Width of source frame.
* @param srcHeight Height of source frame.
* @param dstWidth Width of destination frame.
* @param dstHeight Height of destination frame.
* @param applyRotation Amount of rotation to apply from one frame to another.
* Must be a multiple of 90.
* @param flipHorizontal should flip horizontally
* @param flipVertical should flip vertically
* @param maintainAspectRatio If true, will ensure that scaling in x and y remains constant,
* cropping the image if necessary.
* @return The transformation fulfilling the desired requirements.
*/
public static Matrix getTransformationMatrix(
final int srcWidth,
final int srcHeight,
final int dstWidth,
final int dstHeight,
final int applyRotation, boolean flipHorizontal, boolean flipVertical,
final boolean maintainAspectRatio) {
final Matrix matrix = new Matrix();
if (applyRotation != 0) {
if (applyRotation % 90 != 0) {
throw new IllegalArgumentException(String.format("Rotation of %d % 90 != 0", applyRotation));
}
// Translate so center of image is at origin.
matrix.postTranslate(-srcWidth / 2.0f, -srcHeight / 2.0f);
// Rotate around origin.
matrix.postRotate(applyRotation);
}
// Account for the already applied rotation, if any, and then determine how
// much scaling is needed for each axis.
final boolean transpose = (Math.abs(applyRotation) + 90) % 180 == 0;
final int inWidth = transpose ? srcHeight : srcWidth;
final int inHeight = transpose ? srcWidth : srcHeight;
int flipHorizontalFactor = flipHorizontal ? -1 : 1;
int flipVerticalFactor = flipVertical ? -1 : 1;
// Apply scaling if necessary.
if (inWidth != dstWidth || inHeight != dstHeight) {
final float scaleFactorX = flipHorizontalFactor * dstWidth / (float) inWidth;
final float scaleFactorY = flipVerticalFactor * dstHeight / (float) inHeight;
if (maintainAspectRatio) {
// Scale by minimum factor so that dst is filled completely while
// maintaining the aspect ratio. Some image may fall off the edge.
final float scaleFactor = Math.max(Math.abs(scaleFactorX), Math.abs(scaleFactorY));
matrix.postScale(scaleFactor, scaleFactor);
} else {
// Scale exactly to fill dst from src.
matrix.postScale(scaleFactorX, scaleFactorY);
}
}
if (applyRotation != 0) {
// Translate back from origin centered reference to destination frame.
float dx = dstWidth / 2.0f;
float dy = dstHeight / 2.0f;
matrix.postTranslate(dx, dy);
// postScale中心点如果出错,图像不会被变换
matrix.postScale(flipHorizontalFactor, flipVerticalFactor, dx, dy);
}
return matrix;
}
然后使用上面得到的矩阵来快速变换Bitmap
/*
example
*/
final Canvas canvas = new Canvas(out);
// 这里的空白bitmap尺寸需要与变换后的预期尺寸一致
Bitmap src = Bitmap.createBitmap(height, width, Bitmap.Config.ARGB_8888);
Matrix transformation = getTransformationMatrix(src.getWidth(), src.getHeight(), targetWidth, targetHeight, rotation,true,false, true);
canvas.drawBitmap(src, transformation, null);