【Android】直播必备之YUV使用总结 —— Android常用的几种格式:NV21/NV12/YV12/YUV420P的区别...
YUV图解 (YUV444, YUV422, YUV420, YV12, NV12, NV21)
Android 图像格式ImageFormat 主要参数值说明
Bitmap和byte[]的互相转换
//Bitmap转为byte[]
Bitmap bitmap= BitmapFactory...(资源/byte[]/路径/等)
ByteArrayOutputStream output=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100,output);
byte[] data=output.toByteArray();
//byte[]转换为Bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Bitmap压缩
/**
* 改变bitmap尺寸大小
*
* @param bitmap 原始bitmap
* @param widthNew 新的宽度
* @param heightNew 新的高度
* @return 改变尺寸后的bitmap
*/
private Bitmap changeBitmapSize(Bitmap bitmap, int widthNew, int heightNew) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float widthScale = (float) widthNew / width;
float heightScale = (float) heightNew / height;
Matrix matrix = new Matrix();
matrix.postScale(widthScale, heightScale);
return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
}
二维码相关操作
工具库zxing
private static final String WHITE_COLOR = "ffffff";
/**
* 生成二维码位图
*
* @param text 内容
* @param qrLen 二维码边长
* @return 二维码
* @throws Exception 数据处理异常
*/
public static Bitmap createBitmap(String text, int qrLen) throws Exception {
if (TextUtils.isEmpty(text)) {
return null;
}
Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
//边距
hints.put(EncodeHintType.MARGIN, 1);
//编码格式
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//纠错信息
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, qrLen, qrLen, hints);
//对点阵进行黑白处理
int[] pixels = new int[qrLen * qrLen];
for (int y = 0; y < qrLen; y++) {
for (int x = 0; x < qrLen; x++) {
if (bitMatrix.get(x, y)) {
pixels[y * qrLen + x] = 0xff000000;
} else {
pixels[y * qrLen + x] = 0xffffffff;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(qrLen, qrLen, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, qrLen, 0, 0, qrLen, qrLen);
return bitmap;
}