第一步 在AndroidManifest.xml中加入权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在你的Activity中设置按钮事件打开摄像头
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
System.out.println("用户点击了拍照按钮");
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss");
PhotoFileName =Environment.getExternalStorageDirectory()+File.separator+ format.format(new Date())+"Photo.jpg";
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(PhotoFileName)));
//intent = new Intent(this, com.google.zxing.client.android.CaptureActivity.class);
startActivityForResult(intent, REQUEST_CAMERA_CODE);
重写onActivityResult方法
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode == RESULT_OK){
switch (requestCode){
case REQUEST_CAMERA_CODE:
System.out.println("Get CAMERA RESULT ");
// String LocalPhoto= Environment.getExternalStorageDirectory()+File.separator+PhotoFileName;
Bitmap bitmap = getLoacalBitmap(PhotoFileName);
PhotoResult.setImageBitmap(bitmap);
break;
default:
break;
}
}
}
定义常用变量
private static final int REQUEST_SCAN_BARCODE_CODE = 1;
private static final int REQUEST_CAMERA_CODE = 2;
private static String PhotoFileName="";
TextView QRCodeResult;
ImageView PhotoResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fire_exit_door);
QRCodeResult =(TextView)findViewById(R.id.QRCodeResult) ;
PhotoResult =(ImageView)findViewById(R.id.imageViewPhotoResult);
}