保存图片
public void saveImage(byte[] yuv, int[] rgb , int width, int height) {
Bitmap text_bitmap = null;
int t1 = 0 , t2 = 0 ,t3 = 0 ,t4 = 0 ,t5 = 0 ,t6 = 0 ,t7 = 0 ,t8 = 0 ,t9 = 0 ,t10 = 0 ,t11 = 0;
t1 = 0xff & yuv[0];
t2 = 0xff & yuv[1];
t3 = 0xff & yuv[2];
t4 = 0xff & yuv[3];
t5 = 0xff & yuv[4];
t6 = 0xff & yuv[5];
t7 = 0xff & yuv[6];
t8 = 0xff & yuv[7];
t9 = 0xff & yuv[8];
t10 = 0xff & yuv[9];
t11 = 0xff & yuv[10];
String strT6 = String.format("%02x", t6);//02拿两位十六进制 08拿八位十六进制
String strT10 = String.format("%02x", t10);
String strT11 = String.format("%02x", t11);
// showShortMsg("y= "+strT10+"--"+strT11);
text_bitmap = generateBitmap(t1+"",t2+"",t3+"",t4+"",t5+"","增益:0x"+strT6+"","LED:"+t7+"","亮度:"+t8+"","评分:"+t9+"","曝光:0x"+strT10+""+strT11,"注册:"+Cmd.regtime+"","识别:"+Cmd.identifytime+"",20,Color.RED);
int y = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int dex = (i * width + j)*2;
y = 0xff & yuv[dex];
rgb[i*width + j] = 0xff000000| y << 16 | y << 8 | y;
yuv_y[i*width + j] = yuv[dex];
}
}
Bitmap bmp = Bitmap.createBitmap(rgb,width, height, Bitmap.Config.ARGB_8888);
Bitmap adjustPhotoRotation = adjustPhotoRotation(bmp, 90);
Bitmap combineBitmap = combineBitmap(adjustPhotoRotation, text_bitmap);
if (!TextUtils.isEmpty(text1)&&!TextUtils.isEmpty(text2)&&!TextUtils.isEmpty(text3)&&!TextUtils.isEmpty(text4)&&!TextUtils.isEmpty(text5)&&!TextUtils.isEmpty(text6)&&!TextUtils.isEmpty(text7)) {
fileName = System.currentTimeMillis()+"_"+tv_scene.getText()+"_"+textLh(text1)+"_"+textLh(text2)+"_"+textLh(text3)+"_"+textLh(text4)+"_"+textLh(text5)+"_"+textLh(text6)+"_"+text7 + ".jpg";
}else {
fileName = str = System.currentTimeMillis() +"_"+tv_scene.getText()+"_"+"7005"+"_"+"0301"+"_"+"0420"+"_"+"7005"+"_"+"0301"+"_"+"0420"+"_"+"00"+ ".jpg";
}
File appDir = new File(Environment.getExternalStorageDirectory(), "wy/"+getDate()+"/"+tv_scene.getText());
if (!appDir.exists()) {
appDir.mkdirs();
}
File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
//保存两张bitmap合并之后的jpg图片
combineBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 将 text 转成bitmap
* */
public static Bitmap generateBitmap(String text1,String text2,String text3,String text4,String text5,String text6,String text7,String text8,String text9,String text10,String regtime,String identifytime,int textSizePx,int textColor){
TextPaint textPaint = new TextPaint();
textPaint.setTextSize(textSizePx);
textPaint.setColor(textColor);
// int width = (int) Math.ceil(textPaint.measureText(text));
Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
// int height = (int) Math.ceil(Math.abs(fontMetrics.bottom) + Math.abs(fontMetrics.top));
Bitmap bitmap = Bitmap.createBitmap(960,640, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawText(text1,0,Math.abs(fontMetrics.ascent),textPaint);
canvas.drawText(text2,450,Math.abs(fontMetrics.ascent),textPaint);
canvas.drawText(text3,0,630,textPaint);
canvas.drawText(text4,450,630,textPaint);
canvas.drawText(text5,225,315,textPaint);
canvas.drawText(text6,360,280,textPaint);
canvas.drawText(text7,360,300,textPaint);
canvas.drawText(text8,360,320,textPaint);
canvas.drawText(text9,360,340,textPaint);
canvas.drawText(regtime,360,360,textPaint);
canvas.drawText(identifytime,360,380,textPaint);
canvas.drawText(text10,360,400,textPaint);
return bitmap;
}
/**
* 整张jpg图片顺时针旋转90度
* */
Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {
Matrix m = new Matrix();
m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
try {
Bitmap bm1 = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true);
return bm1;
} catch (OutOfMemoryError ex) {
}
return null;
}
/**
* 合并两张bitmap为一张
*
* @param background
* @param foreground
* @return Bitmap
*/
public static Bitmap combineBitmap(Bitmap background, Bitmap foreground) {
if (background == null) {
return null;
}
int bgWidth = background.getWidth();
int bgHeight = background.getHeight();
// foreground =zoomImg(foreground,200,200);
int fgWidth = foreground.getWidth();
int fgHeight = foreground.getHeight();
Bitmap newmap = Bitmap.createBitmap(bgWidth, bgHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newmap);
canvas.drawBitmap(background, 0, 0, null);
canvas.drawBitmap(foreground, (bgWidth - fgWidth) / 2, (bgHeight - fgHeight) / 2, null);
canvas.save();
canvas.restore();
return newmap;
}