-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathImageConvertor.java
More file actions
75 lines (63 loc) · 2.63 KB
/
ImageConvertor.java
File metadata and controls
75 lines (63 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* COPYRIGHT NOTICE
* Copyright (C) 2015, Jhuster <lujun.hust@gmail.com>
* https://github.com/Jhuster/Android
*
* @license under the Apache License, Version 2.0
*
* @file ImageConvertor.java
* @brief Support NV21/YUY2 RGB JPEG Bitmap Conversion
*
* @version 1.0
* @author Jhuster
* @date 2015/11/20
*/
package com.jhuster.imageprocessor;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.YuvImage;
public class ImageConvertor {
public static byte[] nv21ToJpeg(byte[] nv21,int width, int height) {
YuvImage image = new YuvImage(nv21,ImageFormat.NV21,width,height,null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
image.compressToJpeg(new Rect(0,0,width,height), 100, out);
return out.toByteArray();
}
public static byte[] yuy2ToJpeg(byte[] yuy2,int width, int height) {
YuvImage image = new YuvImage(yuy2,ImageFormat.YUY2,width,height,null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
image.compressToJpeg(new Rect(0,0,width,height), 100, out);
return out.toByteArray();
}
public static Bitmap rgb565ToBitmap(byte[] rgb565, int width, int height) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
bitmap.copyPixelsFromBuffer(ByteBuffer.wrap(rgb565,0,rgb565.length));
return bitmap;
}
public static Bitmap argb8888ToBitmap(byte[] argb, int width, int height) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(ByteBuffer.wrap(argb,0,argb.length));
return bitmap;
}
public static Bitmap jpegToBitmap(byte[] jpeg) {
return BitmapFactory.decodeByteArray(jpeg,0,jpeg.length);
}
public static Bitmap pngToBitmap(byte[] png) {
return BitmapFactory.decodeByteArray(png,0,png.length);
}
public static byte[] bitmapToJpeg(Bitmap bitmap) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, out);
return out.toByteArray();
}
public static byte[] bitmapToPng(Bitmap bitmap) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, out);
return out.toByteArray();
}
}