这期内容当中小编将会给大家带来有关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
代码实现:
/** * 使用照相机拍照 * @author chen.lin * */ 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); } } /** * 拍照对焦 * @param view */ 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(); } } }; /** * 判断照相机是否存在 * @param context * @return */ private boolean checkCameraHardware(Context context){ if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) { return true; } return false; } /** * 初始化照相机 * @return */ 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中怎么实现拍照功能了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。