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流轉(zhuǎn)換為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;
}
}
當(dāng)有了bitmap之后甸饱,如果使用Bitmap.createBitmap()方法來進(jìn)行圖片變換狭姨,速度較慢耀态,推薦使用canvas來變換bitmap
首先需要構(gòu)建正確的變換矩陣(代碼來自于tensorflow官方git)
更新:加入水平翻轉(zhuǎn)與垂直翻轉(zhuǎn)參數(shù)
/**
* 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尺寸需要與變換后的預(yù)期尺寸一致
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);