/**
* Describe: 将NV21数据转成Bitmap数据,便于截图
*/
public class NV21ToBitmap {
private RenderScript rs;
private ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic;
public NV21ToBitmap(Context context) {
rs = RenderScript.create(context);
yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
}
public Bitmap nv21ToBitmap(byte[] nv21, int width, int height) {
Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(nv21.length);
Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height);
Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);
in.copyFrom(nv21);
yuvToRgbIntrinsic.setInput(in);
yuvToRgbIntrinsic.forEach(out);
Bitmap bmpout = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
out.copyTo(bmpout);
return bmpout;
}
}
/**
* 判断指定目录的文件夹是否存在,如果不存在则需要创建新的文件夹
* @param fileName 指定目录
* @return 返回创建结果 TRUE or FALSE
*/
static boolean fileIsExist(String fileName)
{
//传入指定的路径,然后判断路径是否存在
File file=new File(fileName);
if (file.exists()) {
return true;
} else{
//file.mkdirs() 创建文件夹的意思
return file.mkdirs();
}
}
Bitmap图片的操作:截图、保存、旋转、镜像
截图:
Bitmap bitmap = Bitmap.createBitmap(bitmap_background, newRect.left, newRect.top, newRect.width(), newRect.height(), null, false);
保存、旋转、镜像:
public class BitmapUtils {
/**
* Save Bitmap
*
* @param name file name
* @param bm picture to save
*/
public static void saveBitmap(String name, Bitmap bm) {
//指定我们想要存储文件的地址
String TargetPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/images/";
//判断指定文件夹的路径是否存在
if (!FileUtil.fileIsExist(TargetPath)) {
Log.d("test Save Bitmap", "TargetPath isn't exist");
} else {
//如果指定文件夹创建成功,那么我们则需要进行图片存储操作
File saveFile = new File(TargetPath, name);
try {
FileOutputStream saveImgOut = new FileOutputStream(saveFile);
// compress - 压缩的意思
bm.compress(Bitmap.CompressFormat.JPEG, 80, saveImgOut);
// 存储完成后需要清除相关的进程
saveImgOut.flush();
saveImgOut.close();
Log.d("test Save Bitmap", "The picture is save to your phone!");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/**
* 旋转Bitmap
* @param bm
* @param orientationDegree
* @return
*/
public static Bitmap convertRotation(Bitmap bm, final int orientationDegree) {
Matrix m = new Matrix();
m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
float targetX, targetY;
if (orientationDegree == 90) {
targetX = bm.getHeight();
targetY = 0;
} else {
targetX = bm.getHeight();
targetY = bm.getWidth();
}
final float[] values = new float[9];
m.getValues(values);
float x1 = values[Matrix.MTRANS_X];
float y1 = values[Matrix.MTRANS_Y];
m.postTranslate(targetX - x1, targetY - y1);
Bitmap bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
Canvas canvas = new Canvas(bm1);
canvas.drawBitmap(bm, m, paint);
return bm1;
}
/**
* 镜像Bitmap
* @param bitmap
* @return
*/
public static Bitmap convertMirror(Bitmap bitmap)
{
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Bitmap newb = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
Canvas cv = new Canvas(newb);
Matrix m = new Matrix();
//m.postScale(1, -1); //镜像垂直翻转
m.postScale(-1, 1); //镜像水平翻转
//m.postRotate(-90); //旋转-90度
Bitmap new2 = Bitmap.createBitmap(bitmap, 0, 0, w, h, m, true);
cv.drawBitmap(new2, new Rect(0, 0, new2.getWidth(), new2.getHeight()),new Rect(0, 0, w, h), null);
return newb;
}
}