Android Camera拍照

来源:互联网 发布:百分百营销软件总站 编辑:程序博客网 时间:2024/04/25 12:14

//开始拍照

private void startCamera() {
File dir = new File(Environment.getExternalStorageDirectory(), "img");
if (!dir.exists()) {
dir.mkdirs();
}

String fileName = System.currentTimeMillis() + ".jpg";

imgFile = new File(dir, fileName);
if (!imgFile.exists()) {
try {
imgFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
if (!imgFile.exists()) {
imgFile.mkdirs();
}

//在此传入参数Extra_output,直接返回图片可能太大或者是模糊
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imgFile));
startActivityForResult(intent, 100);
}

//拍照回来

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 100:
if (imgFile == null) {
Toast.makeText(this, "img file == null", Toast.LENGTH_LONG).show();
return;
}
// imgView.setImageURI(Uri.fromFile(imgFile));
Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options);

int outWidth = options.outWidth;
int outHeight = options.outHeight;

Log.e(TAG, "height = " + outHeight);
Log.e(TAG, "width = " + outWidth);
options.inJustDecodeBounds = false;
options.inSampleSize = 4;
// 缩小的倍数
Bitmap bitmap2 = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options);
//有些手机系统会把图片自动旋转,如果有旋转,需要转回来
int degrees = readPictureDegree(imgFile.getAbsolutePath());
//图片处理类
Matrix matrix = new Matrix();
matrix.postRotate(degrees);
Bitmap bitmap = Bitmap.createBitmap(bitmap2, 0, 0, bitmap2.getWidth(), bitmap2.getHeight(), matrix, true);
imgView.setImageBitmap(bitmap);
break;
default:
break;
}
}


//读取图像的旋转角度
public int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
0 0
原创粉丝点击