Bitmap 转 RGBA
public static byte[] bitmapToRgba(Bitmap bitmap) {
if (bitmap.getConfig() != Bitmap.Config.ARGB_8888)
throw new IllegalArgumentException("Bitmap must be in ARGB_8888 format");
int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()];
byte[] bytes = new byte[pixels.length * 4];
bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
int i = 0;
for (int pixel : pixels) {
int A = (pixel >> 24) & 0xff;
int R = (pixel >> 16) & 0xff;
int G = (pixel >> 8) & 0xff;
int B = pixel & 0xff;
bytes[i++] = (byte) R;
bytes[i++] = (byte) G;
bytes[i++] = (byte) B;
bytes[i++] = (byte) A;
}
return bytes;
}
RGBA 转 Bitmap
public static Bitmap bitmapFromRgba(int width, int height, byte[] bytes) {
int[] pixels = new int[bytes.length / 4];
int j = 0;
for (int i = 0; i < pixels.length; i++) {
int R = bytes[j++] & 0xff;
int G = bytes[j++] & 0xff;
int B = bytes[j++] & 0xff;
int A = bytes[j++] & 0xff;
int pixel = (A << 24) | (R << 16) | (G << 8) | B;
pixels[i] = pixel;
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
保存 Bitmap 到 SD 卡
public void saveBitmap(int width, int height, ByteBuffer buffer) {
Bitmap bitmap = bitmapFromRgba(width, height, buffer.array());
try {
File dir = mContext.getExternalCacheDir();
boolean mkdir = dir.mkdir();
File file = new File(dir, "/snapshotVideo_" + System.currentTimeMillis() + ".jpg");
boolean newFile = file.createNewFile();
Log.e("xbo", "onSnapshotComplete: newFile = " + newFile);
saveBitmap(file.getAbsolutePath(), bitmap);
} catch (Exception e) {
e.printStackTrace();
Log.e("xbo", "onSnapshotComplete: e = " + e.toString());
}
}
public static File saveBitmap(String fileName, Bitmap bitmap) {
try {
File file = new File(fileName);
OutputStream outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.close();
return file;
} catch (Exception e) {
Log.e("xbo", "saveBitmap: e = " + e.toString());
return null;
}
}
NV21 转 Bitmap
public static Bitmap convertData2(int width, int height, int rotation, int bufferLength, byte[] buffer, int yStride, int uStride, int vStride) {
byte[] NV21 = new byte[bufferLength];
swapYU12toYUV420SemiPlanar(buffer, NV21, width, height, yStride, uStride, vStride);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int[] strides = {yStride, yStride};
YuvImage image = new YuvImage(NV21, ImageFormat.NV21, width, height, strides);
image.compressToJpeg(
new Rect(0, 0, image.getWidth(), image.getHeight()),
100, baos);
Matrix matrix = new Matrix();
matrix.postRotate(rotation);
byte[] bytes = baos.toByteArray();
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Bitmap target = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return target;
}
private static void swapYU12toYUV420SemiPlanar(byte[] yu12bytes, byte[] i420bytes, int width, int height, int yStride, int uStride, int vStride) {
System.arraycopy(yu12bytes, 0, i420bytes, 0, yStride * height);
int startPos = yStride * height;
int yv_start_pos_u = startPos;
int yv_start_pos_v = startPos + startPos / 4;
for (int i = 0; i < startPos / 4; i++) {
i420bytes[startPos + 2 * i + 0] = yu12bytes[yv_start_pos_v + i];
i420bytes[startPos + 2 * i + 1] = yu12bytes[yv_start_pos_u + i];
}
}