调用系统相机:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File dir = new File(dirPath);
image_name = System.currentTimeMillis() + ".png";
if (!dir.exists()) {
boolean iscreat = dir.mkdirs();// 创建照片的存储目录
MyLog.e(TAG, "" + iscreat);
}
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(dir, image_name)));
startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
拍照返回:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
MyLog.v(TAG, "resultCode=" + resultCode);
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
MyLog.v(TAG, "item_id:" + data.getIntExtra("item_id", -1));
}
if (requestCode == REQUEST_CODE_TAKE_PICTURE) {// 拍照返回
}
}
}
图片处理:拍照后的图片有的手机是选择90度的而且图片比较大容易造成内存溢出
public static Bitmap getSmallImage(String pathName, Context context) {
MyLog.v(TAG, "getImage");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[1024 * 16];//设置图片大小16K
options.inJustDecodeBounds = true;
// 获取这个图片的宽和高
Bitmap bitmap = BitmapFactory.decodeFile(pathName, options); // 此时返回bm为空
int maxH = SharePreferenceMng.getInstance(context).getScreenWidth() / 4;
// 计算缩放比
int be = (int) (options.outHeight / (float) maxH);
int ys = options.outHeight % maxH;// 求余数
float fe = ys / (float) maxH;
if (fe >= 0.5)
be = be + 1;
if (be <= 0)
be = 1;
options.inSampleSize = be;
// 重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(pathName, options);
ExifInterface exifInterface;
try {
exifInterface = new ExifInterface(pathName);
int tag = exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION, -1);
if (tag == ExifInterface.ORIENTATION_ROTATE_90) {// 如果是旋转地图片则先旋转,有的相机默认旋转90度
Matrix matrix = new Matrix();
matrix.reset();
matrix.setRotate(90);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
}