1.简单方式(此时得到的图片是缩略图,清晰度比原图差很多)
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (captureIntent.resolveActivity(getPackageManager()) != null)
startActivityForResult(captureIntent, IMAGE_CAPTURE);
然后在onActivityResult里面这么写
if (requestCode == IMAGE_CAPTURE)
if (resultCode == RESULT_OK) {
Bitmap bitmap = (Bitmap)data.getExtras().get("data");
// deal bitmap...
}
2.获取原图
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (captureIntent.resolveActivity(getPackageManager()) != null) {
File imgFile = getImageSaveFile(); //getImageSaveFile可以根据需要写
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imgFile));
startActivityForResult(captureIntent, IMAGE_CAPTURE);
}
下面是getImageSaveFile的一个简单例子(实际使用外存时还应该检测外存是否可读可写,在android6.0上还需要检测是否有读写外存的权限)
String postfix = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date());
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"image_" + postfix);
mImgFilePath = file.getAbsolutePath();
return file;
然后在onActivityResult里这么写
if (requestCode == 3) {
<span style="white-space:pre"> </span>if (resultCode == RESULT_OK) {
ImageView imgView = (ImageView)findViewById(R.id.image); //imgView是我用来测试的,可以忽略
int outWidth = imgView.getWidth();
int outHeight = imgView.getHeight();
Bitmap bitmap = ImageResizer.getImage(mImgFilePath, outWidth, outHeight); //ImageResizer用于图片压缩,因为原图一般很大,可能几M到几十M不等
imgView.setImageBitmap(bitmap);
}
ImageResizer:
public static Bitmap getImage(String path, int widthRequire, int heightRequire) {
BitmapFactory.Options btOptions = new BitmapFactory.Options();
btOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, btOptions);
btOptions.inSampleSize = caculateInSampleSize(btOptions.outWidth, btOptions.outHeight,
widthRequire, heightRequire);
btOptions.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(path, btOptions);
return bitmap;
}
private static int caculateInSampleSize(int outWidth, int outHeight, int widthRequire,
int heightRequire)
{
int inSampleSize = 1;
while (outWidth > widthRequire && outHeight > heightRequire) {
outWidth /= 2;
outHeight /= 2;
inSampleSize *= 2;
}
return inSampleSize;
}
貌似三星手机获取照相原图时图片是颠倒90度的(至少三星s3和s6是这样),我在上面的ImageResizer的getImage方法中log了一下outWidth和outHeight,结果outWidth和outHeight和我预料的正常结果
刚好相反,貌似像素存储的时候就是倒置的,不太懂
可以用下面的方法解决(先获取图片的倒置角度,然后旋转图片)
获取图片倒置的角度:
public static int getCameraPhotoOrientation(String imagePath) {
int rotate = 0;
try {
ExifInterface exif = new ExifInterface(imagePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
return rotate;
}
旋转图片:
public static Bitmap roateBitmap(Bitmap bitmap, int degree) {
if (degree == 0) {
return bitmap;
}
Matrix matrix = new Matrix();
matrix.postRotate(degree);
Bitmap bmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return bmp;
}