在Android开发中,实现拍照功能是一项常见且重要的技能,本文将详细介绍如何在Android应用中实现拍照功能,包括检查设备是否有摄像头、初始化相机、设置预览界面、实现自动对焦及拍照后保存图片到SD卡的方法,以下是具体步骤和代码示例:
检查设备是否有摄像头
需要检查设备是否具备摄像头功能,这可以通过调用系统服务PackageManager
来实现。
private boolean checkCameraHardware(Context context) { if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) { return true; } return false; }
初始化相机
通过调用Camera.open(0)
方法来初始化相机,这里的参数0
表示使用后置摄像头。
public static Camera getCamera() { Camera camera = null; try { camera = Camera.open(0); } catch (Exception e) { e.printStackTrace(); } return camera; }
设置预览界面
为了显示相机的实时画面,需要创建一个预览界面,这里我们使用SurfaceView
来实现。
@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) { // Take care of releasing the Camera preview in your implementation mCamera = null; } }
实现拍照功能
当用户点击拍照按钮时,调用相机的autoFocus
方法进行对焦,然后调用takePicture
方法拍摄照片。
public void takephoto(View view) { mCamera.autoFocus(new AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { mCamera.takePicture(null, null, mPicture); } }); }
拍照后的图片保存
拍摄完成后,通过实现PictureCallback
接口来处理拍摄得到的图片数据,并将其保存到SD卡中。
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(); } } };
完整代码示例
以下是一个完整的Activity代码示例,展示了如何集成上述所有步骤:
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) { // Take care of releasing the Camera preview in your implementation mCamera = null; } } }
相关问题与解答栏目:
问题1:如何在Android中调用系统自带的摄像头进行拍照?
答案: 调用系统自带摄像头进行拍照可以通过启动一个意图(Intent)来实现,在AndroidManifest.xml文件中添加必要的权限声明: ``xml <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" />
` 在主类Java文件中加入以下代码:
`java final int TAKE_PICTURE = 1; //定义请求码 startActivityForResult(new Intent("android.media.action.IMAGE_CAPTURE"), TAKE_PICTURE);
` 处理拍照得到的数据并保存下来:
`java protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) { Bitmap bm = (Bitmap) data.getExtras().get("data"); ImageView img = (ImageView) findViewById(R.id.imageView); img.setImageBitmap(bm); File myCaptureFile = new File("sdcard/123456.jpg"); try { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile)); /*采用压缩转档方法*/ bm.compress(Bitmap.CompressFormat.JPEG, 80, bos); /*调用flush()方法,更新BufferStream*/ bos.flush(); /*结束OutputStream*/ bos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}
`问题2:如何在拍照后直接显示图片到ImageView上?
`java protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) { Bitmap bm = (Bitmap) data.getExtras().get("data"); ImageView img = (ImageView) findViewById(R.id.imageView); img.setImageBitmap(bm); //保存图片到SD卡的逻辑可以在这里添加,如上一个问题的答案所示}}
``
以上就是关于“Android开发实现拍照功能的方法实例解析”的问题,朋友们可以点击主页了解更多内容,希望可以够帮助大家!
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/624747.html