~~~~ 每次用都是重新写,决定把常用的全部保存到网上,下次直接看。
public static boolean GraySaveImageJpeg(byte[] data, int width, int height, String filepath) {
int iSize = width * height;
byte[] bYuv = new byte[iSize * 2];
for(int i = 0; i < iSize; i++){
bYuv[i] = data[i];
bYuv[iSize + i] = (byte)128;
}
try {
YuvImage image = new YuvImage(bYuv, android.graphics.ImageFormat.NV21, width, height, null);
ByteArrayOutputStream os = new ByteArrayOutputStream(bYuv.length);
Rect rect = new Rect(0, 0, width, height);
if (!image.compressToJpeg(rect, 90, os)) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
os.flush();
File file = new File(filepath);
if (!file.exists()) {
file.createNewFile();
}
DataOutputStream to=new DataOutputStream(new FileOutputStream(file));
os.writeTo(to);
os.close();
System.gc();
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
return true;
}