代码块
1.调用相机动作
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
2.用uri 可指定拍照后的保存位置,用于获取清晰图片
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri = Uri.fromFile(new File(path));
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, Photo_Big);
3.回调方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == Photo_Small) {
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
iv.setImageBitmap(bitmap);
}
if (requestCode == Photo_Big) {
FileInputStream is = null;
try {
is = new FileInputStream(path);
Bitmap bitmap = BitmapFactory.decodeStream(is);
iv.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
自定义相机
1.布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<SurfaceView
android:id="@+id/sfv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Click"
android:text="Click!"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
2.代码
public class mCamera extends Activity implements SurfaceHolder.Callback {
private Camera mCamera;
private SurfaceView preview;
private SurfaceHolder mholder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
preview = (SurfaceView) findViewById(R.id.sfv);
mholder = preview.getHolder();
mholder.addCallback(this);
}
@Override
protected void onResume() {
super.onResume();
if (mCamera == null) {
mCamera = GetCamera();
if (mholder != null) {
StartPreview(mCamera, mholder);
}
}
}
@Override
protected void onPause() {
super.onPause();
ReleaseCamera();
}
public void Click(View v) {
}
// 获取硬件相机资源
private Camera GetCamera() {
Camera camera = Camera.open();
return camera;
}
// 开启预览
private void StartPreview(Camera camera, SurfaceHolder holder) {
try {
camera.setPreviewDisplay(holder);
camera.setDisplayOrientation(90);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
// 释放硬件相机资源
private void ReleaseCamera() {
if (mCamera != null) {
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
/* 重写方法 */
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
StartPreview(mCamera, mholder);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera.stopPreview();
StartPreview(mCamera, mholder);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
ReleaseCamera();
}
/* 重写方法 */
}
3.添加 intent-filter
<intent-filter >
<action android:name="android.media.action.IMAGE_CAPTURE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filte`
>
4.添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>