如何在Android camera2上将捕获的照片保存为jpg文件

时间:2018-05-10 02:10:31

标签: android kotlin android-camera2

当我点击捕获按钮

时的当前状态

在Imageview上显示相机预览捕获图像..

但我希望以jpg格式保存相机预览捕获图像。在我的存储空间。

如何保存我的捕获图像?在`/ sdcard / DCIM /'文件夹中。

//click button.
captureBtn.setOnClickListener(View.OnClickListener {
    view -> takePicture()
}

fun takePicture() {
   mCameraDevice?.createCaptureSession(arrayListOf(mImageReader?.surface), mSessionCallback, null)
}

private val mSessionCallback = object : CameraCaptureSession.StateCallback() {
   override fun onConfigured(cameraCaptureSession: CameraCaptureSession?) {
       if (mCameraDevice == null) {
           return
       }

       mCaptureSession = cameraCaptureSession
       val captureBuilder = mCameraDevice?.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE)
       captureBuilder?.addTarget(mImageReader!!.surface)
       mCaptureSession?.capture(captureBuilder?.build(), mCaptureCallback, null)
     }
   }


   private val mCaptureCallback = object : CameraCaptureSession.CaptureCallback() {

    override fun onCaptureProgressed(session: CameraCaptureSession?, request: CaptureRequest?, partialResult: CaptureResult?) {
        Log.d(TAG, "Partial result")
    }

    override fun onCaptureFailed(session: CameraCaptureSession?, request: CaptureRequest?, failure: CaptureFailure?) {
        Log.d(TAG, "Capture session failed")
    }

    override fun onCaptureCompleted(session: CameraCaptureSession?, request: CaptureRequest?, result: TotalCaptureResult?) {
        session?.close()
        mCaptureSession = null
        Log.d(TAG, "Capture session closed")
    }
}

此源显示ImageView上的相机预览捕获。

我想拍摄照片,保存jpg文件。

如何保存捕获图像?

感谢。

1 个答案:

答案 0 :(得分:1)