public static Bitmap rotateBitmap(Bitmap bmp, float degree) {
Matrix matrix = new Matrix();
matrix.postRotate(degree); //旋转
matrix.postScale(-1, 1); //镜像水平翻转
return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
}
Button button_snap = (Button)findViewById(R.id.buttonsnap);
button_snap.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Camera.Size size = mCamera.getParameters().getPreviewSize();
final int w = size.width;
final int h = size.height;
final YuvImage image = new YuvImage(mDataSingle, ImageFormat.NV21, w, h, null);
ByteArrayOutputStream os = new ByteArrayOutputStream(mDataSingle.length);
if( !image.compressToJpeg(new Rect(0,0,w,h), 100, os)){
return;
}
byte[] tmp = os.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(tmp, 0, tmp.length);
Bitmap input = rotateBitmap(bitmap, -90);
Bitmap bmp = input.copy(Bitmap.Config.ARGB_8888, true);
saveBitmap(bmp, "size0.jpg");
int nw = bmp.getWidth();
int nh = bmp.getHeight();
int[] pix = new int[nw * nh];
bmp.getPixels(pix, 0, nw, 0, 0, nw, nh);
int[] resultPixels = mFaceRecognizer.bitmapFace(pix, nw, nh); //返回结果
Bitmap result = Bitmap.createBitmap(nw, nh, Bitmap.Config.ARGB_8888);
result.setPixels(resultPixels,0,nw,0,0,nw,nh);
imageSnapFace.setImageBitmap(result);
saveBitmap(result, "result.jpg");
}
});
public static Bitmap getScaledBitmap(Bitmap m_img, float sx, float sy) {
Matrix matrix = new Matrix();
matrix.postScale(sx, sy);
Bitmap rst = Bitmap.createBitmap(m_img, 0, 0, m_img.getWidth(), m_img.getHeight(), matrix, true);
return rst;
}
img = getScaledBitmap(imgTemp, 0.1f, 0.1f); //缩小图片
imageView.setImageBitmap(img);
//保存图片
public static void saveBitmap(Bitmap bm, String picName) {
File f = new File("/storage/emulated/0/Pictures/", picName);
if (f.exists()) {
f.delete();
}
try {
FileOutputStream out = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
saveBitmap(img, "MyScaleImg.png"); //保存图片
完整版:
//缩放图片大小
public static Bitmap getScaledBitmap(Bitmap m_img, float sx, float sy) {
Matrix matrix = new Matrix();
matrix.postScale(sx, sy);
Bitmap rst = Bitmap.createBitmap(m_img, 0, 0, m_img.getWidth(), m_img.getHeight(), matrix, true);
return rst;
}
//保存图片
public static void saveBitmap(Bitmap bm, String picName) {
File f = new File("/storage/emulated/0/Pictures/", picName);
if (f.exists()) {
f.delete();
}
try {
FileOutputStream out = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onDestroy(){
super.onDestroy();
detecter.release(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == PICTURE_CHOOSE) {
if (intent != null) {
try {
//加载图片并显示
ContentResolver resolver = getContentResolver();
Uri originalUri = intent.getData();//获得图片的uri
Bitmap imgTemp = MediaStore.Images.Media.getBitmap(resolver, originalUri);
img = getScaledBitmap(imgTemp, 0.3f, 0.3f); //-----------缩小图片
imageView.setImageBitmap(img);
//获取图片的路径
Cursor cursor = getContentResolver().query(intent.getData(), null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(ImageColumns.DATA);
String fileSrc = cursor.getString(idx);
textViewDebug.setText("Picture:" + fileSrc);
textView.setText("图片加载成功!");
buttonDetect.setVisibility(View.VISIBLE);
saveBitmap(img, "MyScaleImg.png"); //---------------------保存图片
}catch (IOException e) {
textView.setText("TAG-->Error" + e.toString());
}
}
else {
textViewDebug.setText("intent == null");
}
}
}//onActivityResult()
//保存图片
public static void saveBitmap(Bitmap bm, String picName) {
File f = new File("/storage/emulated/0/Pictures/", picName);
if (f.exists()) {
f.delete();
}
try {
FileOutputStream out = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//length用户要求产生字符串的长度
public static String getRandomString(int length){
String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random=new Random();
StringBuffer sb=new StringBuffer();
for(int i=0;i<length;i++){
int number=random.nextInt(62);
sb.append(str.charAt(number));
}
return sb.toString();
}