在Android中,可以通过Intent调用系统相机进行拍照,并获取拍照结果。
在Android应用开发中,调用摄像头进行拍照功能是一个常见的需求,本文将详细介绍如何在Android应用中实现这一功能,包括权限申请、相机启动、拍照处理以及图片保存等步骤。
一、权限申请
在Android 6.0(API级别23)及以上版本中,需要在运行时请求用户授予相机和存储权限,在AndroidManifest.xml
文件中声明所需权限:
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在代码中动态请求权限:
private void requestPermissions() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE); } else { openCamera(); } }
二、相机启动
创建相机实例并启动预览:
private Camera mCamera; private CameraPreview mPreview; private void openCamera() { mCamera = getCameraInstance(); mPreview = new CameraPreview(this, mCamera); FrameLayout preview = findViewById(R.id.camera_preview); preview.addView(mPreview); } private Camera getCameraInstance() { CameraCharacteristics characteristics = null; CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); try { String cameraId = manager.getCameraIdList()[0]; // 使用后置摄像头 characteristics = manager.getCameraCharacteristics(cameraId); StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); assert map != null; Size largest = Collections.max(Arrays.asList(map.getOutputSizes(ImageFormat.YUV_420_888)), new CompareSizesByArea()); mCamera = manager.openCamera(cameraId, stateCallback, null); } catch (CameraAccessException e) { e.printStackTrace(); } return mCamera; }
三、拍照处理
设置拍照按钮点击事件监听器,捕获拍照瞬间:
Button captureButton = findViewById(R.id.button_capture); captureButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { takePicture(); } }); private void takePicture() { mCamera.takePicture(null, null, mPicture); }
定义回调接口处理拍照结果:
private final ImageReader.OnImageAvailableListener mPicture = new ImageReader.OnImageAvailableListener() { @Override public void onImageAvailable(ImageReader reader) { Bitmap bitmap = mBitmap; File pictureFile = getOutputMediaFile(); if (pictureFile == null) { return; } try (FileOutputStream fos = new FileOutputStream(pictureFile)) { bitmap.compress(Bitmap.CompressFormat.JPEG, QUALITY, 100, fos); } catch (IOException e) { e.printStackTrace(); } } };
四、图片保存
定义方法生成唯一的文件名并保存图片:
private File getOutputMediaFile() { File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp"); if (!mediaStorageDir.exists()) { if (!mediaStorageDir.mkdirs()) { return null; } } String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); File mediaFile; = new File(mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp +".jpg"); return mediaFile; }
五、完整代码示例
以下是完整的代码示例,展示了如何在Android应用中实现调用摄像头进行拍照功能:
public class MainActivity extends AppCompatActivity { private static final int PERMISSION_REQUEST_CODE = 100; private Camera mCamera; private CameraPreview mPreview; private final ImageReader.OnImageAvailableListener mPicture = new ImageReader.OnImageAvailableListener() { @Override public void onImageAvailable(ImageReader reader) { Bitmap bitmap = mBitmap; File pictureFile = getOutputMediaFile(); if (pictureFile == null) { return; } try (FileOutputStream fos = new FileOutputStream(pictureFile)) { bitmap.compress(Bitmap.CompressFormat.JPEG, QUALITY, 100, fos); } catch (IOException e) { e.printStackTrace(); } } }; // ...其他代码... }
到此,以上就是小编对于“Android实现调用摄像头进行拍照功能”的问题就介绍到这了,希望介绍的几点解答对大家有用,有任何问题和不懂的,欢迎各位朋友在评论区讨论,给我留言。