这期内容当中小编将会给大家带来有关Android中怎么实现拍照功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
解析:
1)判断是否有摄像头checkCameraHardware(this)
2)获得相机camera = Camera.open(0);
3)把相机添加到mPreView = new SurfacePreView(this, mCamera);
4)实现拍照 mCamera.autoFocus
5)在拍照后使用mCamera.takePicture(null, null, mPicture);方法把图片保存到sdcard
代码实现:
public class CameraActivity extends Activity { private Camera mCamera;//相机 private SurfacePreView mPreView;//视图 private FrameLayout mFrameLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera_takephoto); mFrameLayout = (FrameLayout) findViewById(R.id.framelayout); if (checkCameraHardware(this)) { mCamera = getCamera(); mPreView = new SurfacePreView(this, mCamera); mFrameLayout.addView(mPreView); } } public void takephoto(View view){ mCamera.autoFocus(new AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { mCamera.takePicture(null, null, mPicture); } }); } private PictureCallback mPicture = new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { try { File file = new File("/sdcard/file/aa.jpg"); if (!file.exists()) { file.mkdirs(); } FileOutputStream fos = new FileOutputStream(file); fos.write(data); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }; private boolean checkCameraHardware(Context context){ if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) { return true; } return false; } public static Camera getCamera(){ Camera camera = null; try { camera = Camera.open(0); } catch (Exception e) { e.printStackTrace(); } return camera; } @SuppressWarnings("deprecation") private class SurfacePreView extends SurfaceView implements SurfaceHolder.Callback{ private Camera mCamera; private SurfaceHolder mHolder; public SurfacePreView(Context context, Camera camera) { super(context); mCamera = camera; mHolder = getHolder(); mHolder.addCallback(this); mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } @Override public void surfaceCreated(SurfaceHolder holder) { try { mCamera.setPreviewDisplay(mHolder); mCamera.startPreview(); } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if (holder.getSurface() == null) { return; } try { mCamera.stopPreview(); mCamera.setPreviewDisplay(mHolder); mCamera.startPreview(); } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { // mCamera = null; } }}
布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/framelayout" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" > </FrameLayout> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="takephoto" android:text="拍照" /></LinearLayout>
上述就是小编为大家分享的Android中怎么实现拍照功能了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注编程网行业资讯频道。