调用系统摄像头来实现拍照功能
首先,设置布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.dingding.mucapture.MainActivity">
<Button
android:id="@+id/btn_capture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="使用系统拍照功能" />
<ImageView
android:id="@+id/img_capture"
android:layout_width="320dp"
android:layout_height="320dp" />
</LinearLayout>
MainActivity.java :
1.使用系统拍照功能并显示
2.使用自定义拍照路径并解析显示
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_capture;
private ImageView img_capture;
private Uri outputUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_capture = (Button) findViewById(R.id.btn_capture);
img_capture = (ImageView) findViewById(R.id.img_capture);
btn_capture.setOnClickListener(this);
}
private final int TAKE_CAPTURE = 1;
@Override
public void onClick(View v) {
switch (v.getId()) {
//获取系统拍照功能
case R.id.btn_capture:
//------------------ 1 ----------------------------------------------
// Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// startActivityForResult(intent, TAKE_CAPTURE);
//------------------ 2 ----------------------------------------------
//自定义拍照路径
File file = new File(Environment.getExternalStorageDirectory(), "my.png");
outputUri = Uri.fromFile(file);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
startActivityForResult(intent, TAKE_CAPTURE);
break;
}
}
/*
* 获取照片
* 覆写startActivityForResult中的方法*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == TAKE_CAPTURE) {
//------------------ 1 ----------------------------------------------
//解析照片
if (data != null) {
//解析数据
if (data.hasExtra("data")) {
Bitmap bitmap = data.getParcelableExtra("data");
img_capture.setImageBitmap(bitmap);
}
}
//------------------ 2 ----------------------------------------------
//读取图片,并进行缩放
else {
int width = img_capture.getWidth();
int height = img_capture.getHeight();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(outputUri.getPath(), options);
//图片真正的宽高
int imgWidth = options.outWidth;
int imgHeight = options.outHeight;
//缩放系数
int scale = Math.min(imgWidth / width, imgHeight / height);
scale = scale == 0 ? 1 : scale;
options.inJustDecodeBounds = false;
options.inSampleSize = scale;
Bitmap bitmap = BitmapFactory.decodeFile(outputUri.getPath(), options);
img_capture.setImageBitmap(bitmap);
}
}
}
}
}