entnal提问: 怎么用程序实现调用Android手机的拍照功能
小编推荐答案:
publicclasstestActivityextendsActivity{
publicstaticfinalintNONE=0;
publicstaticfinalintPHOTOHRAPH=1;//拍照
publicstaticfinalintPHOTOZOOM=2;//缩放
publicstaticfinalintPHOTORESOULT=3;//结果
publicstaticfinalStringIMAGE_UNSPECIFIED="image/*";
ImageViewimageView=null;
Buttonbutton0=null;
Buttonbutton1=null;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView=(ImageView)findViewById(R.id.imageID);
button0=(Button)findViewById(R.id.btn_01);
button1=(Button)findViewById(R.id.btn_02);
button0.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
Intentintent=newIntent(Intent.ACTION_PICK,null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,IMAGE_UNSPECIFIED);
startActivityForResult(intent,PHOTOZOOM);
}
});
button1.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
Intentintent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(newFile(Environment.getExternalStorageDirectory(),"temp.jpg")));
startActivityForResult(intent,PHOTOHRAPH);
}
});
}
@Override
protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
if(resultCode==NONE)
return;
//拍照
if(requestCode==PHOTOHRAPH){
//设置文件保存路径这里放在跟目录下
Filepicture=newFile(Environment.getExternalStorageDirectory()+"/temp.jpg");
startPhotoZoom(Uri.fromFile(picture));
}
if(data==null)
return;
//读取相册缩放图片
if(requestCode==PHOTOZOOM){
startPhotoZoom(data.getData());
}
//处理结果
if(requestCode==PHOTORESOULT){
Bundleextras=data.getExtras();
if(extras!=null){
Bitmapphoto=extras.getParcelable("data");
ByteArrayOutputStreamstream=newByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG,75,stream);//(0-100)压缩文件
imageView.setImageBitmap(photo);
}
}
super.onActivityResult(requestCode,resultCode,data);
}
publicvoidstartPhotoZoom(Uriuri){
Intentintent=newIntent("com.android.camera.action.CROP");
intent.setDataAndType(uri,IMAGE_UNSPECIFIED);
intent.putExtra("crop","true");
//aspectXaspectY是宽高的比例
intent.putExtra("aspectX",1);
intent.putExtra("aspectY",1);
//outputXoutputY是裁剪图片宽高
intent.putExtra("outputX",64);
intent.putExtra("outputY",64);
intent.putExtra("return-data",true);
startActivityForResult(intent,PHOTORESOULT);
}
}