Android调用系统相机Camera进行拍照程序详解(拍照及本地存储)

第一步:先在 AndroidManifest.xml 添加照相机权限和文件读取权限,具体如下,在mainfest标签与application标签之间添加

<uses-permission android:name="android.permission.CAMERA"/>          
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

第二步:拍照按钮响应事件,代码如下

//button_camera为拍照按钮ID

button_camera.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //通过Intent方式调用系统相机
        Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //获取根目录,没有外存的话,就是系统根目录
        String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        //给拍照获取的图像命名
        String name = System.currentTimeMillis() + ".jpg";
        //图像存放路径
        imagePath = new File(rootPath + "/拍照按钮文件夹_" +新拍照片+ "/", name);
        //判断该路径是否存在,不存在则创建
        if (!imagePath.exists()) {
            imagePath.getParentFile().mkdir();
        }
        // 设置图片输出路径
        imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imagePath));
        //  imageCaptureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // 图片质量
        QuestionActivity.this.startActivityForResult(imageCaptureIntent, 1);
    }
});

运行程序后,在手机“文件管理”根目录下,可以发现“拍照按钮文件夹”,该文件夹下就是刚才所拍的照片。

本程序仅仅涉及到如何调用相机拍照及照片的存放,不涉及所拍照片的显示与预览等功能,后续将不断完善。

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
android调用camera时,可以自己写一个activity,赋上相关参数,打开前camera就可以了; 需要申请的permission,在AndroidManifest.xml中添加: 主要功能,打开前camera private Camera openFrontFacingCameraGingerbread() { int cameraCount = 0; Camera cam = null; Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); cameraCount = Camera.getNumberOfCameras(); for (int camIdx = 0; camIdx < cameraCount; camIdx++) { Camera.getCameraInfo(camIdx, cameraInfo); if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { try { cam = Camera.open(camIdx); mCurrentCamIndex = camIdx; } catch (RuntimeException e) { Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage()); } } } return cam; } 根据打开时的横竖屏方向来调整preview角度 //根据横竖屏自动调节preview方向,Starting from API level 14, this method can be called when preview is active. private static void setCameraDisplayOrientation(Activity activity,int cameraId, Camera camera) { Camera.CameraInfo info = new Camera.CameraInfo(); Camera.getCameraInfo(cameraId, info); int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); //degrees the angle that the picture will be rotated clockwise. Valid values are 0, 90, 180, and 270. //The starting position is 0 (landscape). int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; case Surface.ROTATION_90: degrees = 90; break; case Surface.ROTATION_180: degrees = 180; break; case Surface.ROTATION_270: degrees = 270; break; } int result; if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { result = (info.orientation + degrees) % 360; result = (360 - result) % 360; // compensate the mirror } else { // back-facing result = (info.orientation - degrees + 360) % 360; } camera.setDisplayOrientation(result); }
可以按照以下步骤来实现: 1. 在 `build.gradle` 文件中添加依赖: ``` implementation "androidx.camera:camera-core:1.0.0" implementation "androidx.camera:camera-camera2:1.0.0" ``` 2. 在布局文件中添加一个按钮: ``` <Button android:id="@+id/btn_take_picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Take Picture" /> ``` 3. 在 Activity 或 Fragment 中初始化 CameraX: ```kotlin private lateinit var imageCapture: ImageCapture private fun startCamera() { val cameraProviderFuture = ProcessCameraProvider.getInstance(this) cameraProviderFuture.addListener({ val cameraProvider = cameraProviderFuture.get() val preview = Preview.Builder().build().also { it.setSurfaceProvider(viewFinder.surfaceProvider) } imageCapture = ImageCapture.Builder().build() val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA try { cameraProvider.unbindAll() cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture) } catch (e: Exception) { Log.e(TAG, "Use case binding failed", e) } }, ContextCompat.getMainExecutor(this)) } ``` 这里我们使用了 CameraX 的 `ImageCapture` 用例,该用例可以用于拍照。 4. 在按钮点击事件中触发拍照操作: ```kotlin btn_take_picture.setOnClickListener { val file = File(externalMediaDirs.first(), "${System.currentTimeMillis()}.jpg") val outputOptions = ImageCapture.OutputFileOptions.Builder(file).build() imageCapture.takePicture(outputOptions, ContextCompat.getMainExecutor(this), object : ImageCapture.OnImageSavedCallback { override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) { val savedUri = Uri.fromFile(file) // TODO: 处理保存成功后的操作 } override fun onError(exception: ImageCaptureException) { Log.e(TAG, "Photo capture failed: ${exception.message}", exception) } }) } ``` 在这里我们将拍摄的照片保存到了外部存储器中,并且在保存成功后得到了保存的文件路径。 5. 添加相机权限: ```xml <uses-permission android:name="android.permission.CAMERA" /> ``` 并在运行时动态请求相机权限。 以上是使用 CameraX 系统相机拍照的基本步骤,你可以根据需要进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值