首页 >> 大全

十分钟实现 Android Camera2 相机拍照

2023-08-02 大全 21 作者:考证青年

1. 前言

因为工作中要使用 API,但因为比较复杂,网上资料也比较乱,有一定入门门槛,所以花了几天时间系统研究了下,并在CSDN上记录了下,希望能帮助到更多的小伙伴。

上篇文章 我们使用实现了相机预览的功能,这篇文章我们接着上文,来实现相机拍照的功能。

2. 前置操作 2.1 声明相机参数和成员变量

首先还是声明相机参数和成员变量,比起前文增加了这些

private lateinit var imageReader: ImageReader
//JPEG格式,所有相机必须支持JPEG输出,因此不需要检查
private val pixelFormat = ImageFormat.JPEG
//imageReader最大的图片缓存数
private val IMAGE_BUFFER_SIZE: Int = 3
//线程池
private val threadPool = Executors.newCachedThreadPool()
private val imageReaderThread = HandlerThread("imageReaderThread").apply { start() }
private val imageReaderHandler = Handler(imageReaderThread.looper)

完整的需要声明的相机参数和成员变量如下

//后摄 : 0 ,前摄 : 1
private val cameraId = "0"
private val TAG = CameraActivity::class.java.simpleName
private lateinit var cameraDevice: CameraDevice
private val cameraThread = HandlerThread("CameraThread").apply { start() }
private val cameraHandler = Handler(cameraThread.looper)
private val cameraManager: CameraManager by lazy {getSystemService(Context.CAMERA_SERVICE) as CameraManager
}
private val characteristics: CameraCharacteristics by lazy {cameraManager.getCameraCharacteristics(cameraId)
}
private lateinit var session: CameraCaptureSessionprivate lateinit var imageReader: ImageReader
//JPEG格式,所有相机必须支持JPEG输出,因此不需要检查
private val pixelFormat = ImageFormat.JPEG
//imageReader最大的图片缓存数
private val IMAGE_BUFFER_SIZE: Int = 3
//线程池
private val threadPool = Executors.newCachedThreadPool()
private val imageReaderThread = HandlerThread("imageReaderThread").apply { start() }
private val imageReaderHandler = Handler(imageReaderThread.looper)

2.2 初始化

我们需要在合适的时机去初始化

这里我们把它放到中

private fun startPreview() {// Initialize an image reader which will be used to capture still photosval size = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)!!.getOutputSizes(pixelFormat).maxByOrNull { it.height * it.width }!!imageReader = ImageReader.newInstance(size.width, size.height, pixelFormat, IMAGE_BUFFER_SIZE)//...原本的代码...
}

2.3 将关联到

首先我们要在()方法里面,修改

原本的,只传入了...

val targets = listOf(binding.surfaceView.holder.surface)

现在要多传入一个.

val targets = listOf(binding.surfaceView.holder.surface,imageReader.surface)

完整代码如下

private fun startPreview() {fitSize = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)!!.getOutputSizes(pixelFormat).maxByOrNull { it.height * it.width }!!imageReader = ImageReader.newInstance(fitSize.width, fitSize.height, pixelFormat, IMAGE_BUFFER_SIZE)val targets = listOf(binding.surfaceView.holder.surface,imageReader.surface)cameraDevice.createCaptureSession(targets, object : CameraCaptureSession.StateCallback() {override fun onConfigured(session: CameraCaptureSession) {this@CameraActivity2.session = sessionval captureRequest = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW).apply { addTarget(binding.surfaceView.holder.surface) }//这将不断地实时发送视频流,直到会话断开或调用session.stoprepeat()session.setRepeatingRequest(captureRequest.build(), null, cameraHandler)}override fun onConfigureFailed(session: CameraCaptureSession) {Toast.makeText(application, "session configuration failed", Toast.LENGTH_SHORT).show()}}, cameraHandler)
}

3. 实现拍照功能 3.1 清空

首先需要清空,防止里还有缓存

// Flush any images left in the image readerwhile (imageReader.acquireNextImage() != null) {}

3.2 设置ener监听

怎么样实现手机虚拟拍照_美拍照相机怎么拍照_

然后创建一个新的队列Queue,调用注册一个监听器,在中有新图像可用时调用。

//Start a new image queue
val imageQueue = ArrayBlockingQueue<Image>(IMAGE_BUFFER_SIZE)

给设置一个ener监听

一共有两个参数,第一个参数是ener接口,第二个参数是,这里我们传入即可。

ener监听里,会去获取的下一个image,并添加到

imageReader.setOnImageAvailableListener({ reader ->val image = reader.acquireNextImage()Log.d(TAG, "Image available in queue: ${image.timestamp}")imageQueue.add(image)}, imageReaderHandler)

3.3 创建.

接着,通过..创建.

val captureRequest = session.device.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE).apply { addTarget(imageReader.surface) }

3.4 调用调用.()执行拍照

然后调用.()来进行拍照,需要传入、.回调和

session.capture(captureRequest.build(),object : CameraCaptureSession.CaptureCallback() {override fun onCaptureCompleted(session: CameraCaptureSession,request: CaptureRequest,result: TotalCaptureResult) {super.onCaptureCompleted(session, request, result)//待实现}},cameraHandler
)

3.5 拍照回调处理

当调用后,会调用异步线程,然后从中取出image,并把监听设为null

然后调用方法,将图片保存到本地存储中。

threadPool.execute {val image = imageQueue.take()imageReader.setOnImageAvailableListener(null, null)val file = saveImage(image)if (file.exists()) {runOnUiThread {Toast.makeText(application, "拍照成功", Toast.LENGTH_SHORT).show()}}
}

3.6 将图片保存到本地

来看下方法

首先会判断是否是JPEG 格式,如果是JPEG格式,那就简单地保存bytes即可

如果不是JPEG格式,本文就略过未实现了,有需要的小伙伴可以去看下官方Demo。

private fun saveImage(image: Image): File {when (image.format) {//当format是JPEG或PEPTH JPEG时,我们可以简单地保存bytesImageFormat.JPEG, ImageFormat.DEPTH_JPEG -> {val buffer = image.planes[0].bufferval bytes = ByteArray(buffer.remaining()).apply { buffer.get(this) }try {val output = createFile(this@CameraActivity2, "jpg")FileOutputStream(output).use { it.write(bytes) }return output} catch (exc: IOException) {Log.e(TAG, "Unable to write JPEG image to file", exc)throw exc}}//本示例未实现其他格式else -> {val exc = RuntimeException("Unknown image format: ${image.format}")throw exc}}
}

这里的用来获取一个文件路径

fun createFile(context: Context, extension: String): File {val sdf = SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS", Locale.US)val imageDir = context.getExternalFilesDir("image")return File(imageDir, "IMG_${sdf.format(Date())}.$extension")
}

3.7 拍照部分完整代码

怎么样实现手机虚拟拍照_美拍照相机怎么拍照_

再来看一下拍照部分完整的代码

binding.btnTakePicture.setOnClickListener {// Flush any images left in the image reader@Suppress("ControlFlowWithEmptyBody")while (imageReader.acquireNextImage() != null) {}// Start a new image queueval imageQueue = ArrayBlockingQueue<Image>(IMAGE_BUFFER_SIZE)imageReader.setOnImageAvailableListener({ reader ->val image = reader.acquireNextImage()Log.d(TAG, "Image available in queue: ${image.timestamp}")imageQueue.add(image)}, imageReaderHandler)val captureRequest = session.device.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE).apply { addTarget(imageReader.surface) }session.capture(captureRequest.build(),object : CameraCaptureSession.CaptureCallback() {override fun onCaptureCompleted(session: CameraCaptureSession,request: CaptureRequest,result: TotalCaptureResult) {super.onCaptureCompleted(session, request, result)threadPool.execute {val image = imageQueue.take()imageReader.setOnImageAvailableListener(null, null)val file = saveImage(image)if (file.exists()) {runOnUiThread {Toast.makeText(application, "拍照成功", Toast.LENGTH_SHORT).show()}}}}},cameraHandler)
}private fun saveImage(image: Image): File {when (image.format) {//当format是JPEG或PEPTH JPEG时,我们可以简单地保存bytesImageFormat.JPEG, ImageFormat.DEPTH_JPEG -> {val buffer = image.planes[0].bufferval bytes = ByteArray(buffer.remaining()).apply { buffer.get(this) }try {val output = createFile(this@CameraActivity2, "jpg")FileOutputStream(output).use { it.write(bytes) }return output} catch (exc: IOException) {Log.e(TAG, "Unable to write JPEG image to file", exc)throw exc}}//本示例未实现其他格式else -> {val exc = RuntimeException("Unknown image format: ${image.format}")throw exc}}
}fun createFile(context: Context, extension: String): File {val sdf = SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS", Locale.US)val imageDir = context.getExternalFilesDir("image")return File(imageDir, "IMG_${sdf.format(Date())}.$extension")
}

我们运行程序,点击按钮拍照,可以发现提示拍照成功

我们打开文件管理器,在///data/包名/files/image文件夹下,可以看到这张图片

但是我们发现这张照片的方向是不对的

4. 修正图片方向

我们可以看到之前拍摄的图片,方向是不对的,所以需要对图片的方向进行修正

首先添加这个类

import android.content.Context
import android.hardware.camera2.CameraCharacteristics
import android.view.OrientationEventListener
import android.view.Surface
import androidx.lifecycle.LiveData/*** Calculates closest 90-degree orientation to compensate for the device* rotation relative to sensor orientation, i.e., allows user to see camera* frames with the expected orientation.*/
class OrientationLiveData(context: Context,characteristics: CameraCharacteristics
): LiveData<Int>() {private val listener = object : OrientationEventListener(context.applicationContext) {override fun onOrientationChanged(orientation: Int) {val rotation = when {orientation <= 45 -> Surface.ROTATION_0orientation <= 135 -> Surface.ROTATION_90orientation <= 225 -> Surface.ROTATION_180orientation <= 315 -> Surface.ROTATION_270else -> Surface.ROTATION_0}val relative = computeRelativeRotation(characteristics, rotation)if (relative != value) postValue(relative)}}override fun onActive() {super.onActive()listener.enable()}override fun onInactive() {super.onInactive()listener.disable()}companion object {/*** Computes rotation required to transform from the camera sensor orientation to the* device's current orientation in degrees.** @param characteristics the [CameraCharacteristics] to query for the sensor orientation.* @param surfaceRotation the current device orientation as a Surface constant* @return the relative rotation from the camera sensor to the current device orientation.*/@JvmStaticprivate fun computeRelativeRotation(characteristics: CameraCharacteristics,surfaceRotation: Int): Int {val sensorOrientationDegrees =characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION)!!val deviceOrientationDegrees = when (surfaceRotation) {Surface.ROTATION_0 -> 0Surface.ROTATION_90 -> 90Surface.ROTATION_180 -> 180Surface.ROTATION_270 -> 270else -> 0}// Reverse device orientation for front-facing camerasval sign = if (characteristics.get(CameraCharacteristics.LENS_FACING) ==CameraCharacteristics.LENS_FACING_FRONT) 1 else -1// Calculate desired JPEG orientation relative to camera orientation to make// the image upright relative to the device orientationreturn (sensorOrientationDegrees - (deviceOrientationDegrees * sign) + 360) % 360}}
}

在中声明,并注册观察者,当方向改变会通知。

// Used to rotate the output media to match device orientation
relativeOrientation = OrientationLiveData(this, characteristics).apply {observe(this@CameraActivity2, Observer { orientation ->Log.d(TAG, "Orientation changed: $orientation")})
}

接着在回调里,之后,添加如下代码来修改图片的方向

// Compute EXIF orientation metadata
val rotation = relativeOrientation.value ?: 0
val mirrored = characteristics.get(CameraCharacteristics.LENS_FACING) ==CameraCharacteristics.LENS_FACING_FRONT
val exifOrientation = computeExifOrientation(rotation, mirrored)val exif = ExifInterface(file.absolutePath)
exif.setAttribute(ExifInterface.TAG_ORIENTATION, exifOrientation.toString()
)
exif.saveAttributes()

/** Transforms rotation and mirroring information into one of the [ExifInterface] constants */
fun computeExifOrientation(rotationDegrees: Int, mirrored: Boolean) = when {rotationDegrees == 0 && !mirrored -> ExifInterface.ORIENTATION_NORMALrotationDegrees == 0 && mirrored -> ExifInterface.ORIENTATION_FLIP_HORIZONTALrotationDegrees == 180 && !mirrored -> ExifInterface.ORIENTATION_ROTATE_180rotationDegrees == 180 && mirrored -> ExifInterface.ORIENTATION_FLIP_VERTICALrotationDegrees == 90 && !mirrored -> ExifInterface.ORIENTATION_ROTATE_90rotationDegrees == 90 && mirrored -> ExifInterface.ORIENTATION_TRANSPOSErotationDegrees == 270 && !mirrored -> ExifInterface.ORIENTATION_ROTATE_270rotationDegrees == 270 && mirrored -> ExifInterface.ORIENTATION_TRANSVERSE//rotationDegrees == 270 && mirrored -> ExifInterface.ORIENTATION_TRANSVERSE//rotationDegrees == 270 && mirrored -> ExifInterface.ORIENTATION_ROTATE_270//rotationDegrees == 270 && !mirrored -> ExifInterface.ORIENTATION_TRANSVERSEelse -> ExifInterface.ORIENTATION_UNDEFINED
}

再来运行程序,看到图片的方向就正常了

5. 销毁相机

当销毁的时候,我们也要去销毁相机,比起上篇文章,多了个的销毁

override fun onStop() {super.onStop()try {cameraDevice.close()} catch (exc: Throwable) {Log.e(TAG, "Error closing camera", exc)}
}override fun onDestroy() {super.onDestroy()cameraThread.quitSafely()imageReaderThread.quitSafely()
}

至此我们就用完成相机拍照功能了。

6. 其他 6.1 本文源码下载

下载地址 : Demo - 实现相机预览、拍照、录制视频功能

6.2 系列

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了