十分钟实现 Android Camera2 相机拍照

news/2024/11/22 17:46:57/

1. 前言

因为工作中要使用Android Camera2 API,但因为Camera2比较复杂,网上资料也比较乱,有一定入门门槛,所以花了几天时间系统研究了下,并在CSDN上记录了下,希望能帮助到更多的小伙伴。
上篇文章 我们使用Camera2实现了相机预览的功能,这篇文章我们接着上文,来实现Camera2相机拍照的功能。

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 初始化imageReader

我们需要在合适的时机去初始化imageReader
这里我们把它放到startPreview

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 将imageReader关联到Session

首先我们要在startPreview()方法里面,修改targets
原本的targets,只传入了binding.surfaceView.holder.surface

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

现在要多传入一个imageReader.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 清空imageReader

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

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

3.2 设置OnImageAvailableListener监听

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

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

imageReader设置一个OnImageAvailableListener监听
setOnImageAvailableListener一共有两个参数,第一个参数是OnImageAvailableListener接口,第二个参数是Handler,这里我们传入imageReaderHandler即可。
OnImageAvailableListener监听里,会去获取imageReader的下一个image,并添加到imageQueue

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

3.3 创建CaptureRequest.Builder

接着,通过session.device.createCaptureRequest创建CaptureRequest.Builder

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

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

然后调用session.capture()来进行拍照,需要传入captureRequestCameraCaptureSession.CaptureCallback回调和Handler

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

3.5 拍照回调处理

onCaptureCompleted调用后,会调用异步线程,然后从imageQueue中取出image,并把setOnImageAvailableListener监听设为null
然后调用saveResult方法,将图片保存到本地存储中。

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 将图片保存到本地

来看下saveImage方法
首先会判断是否是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}}
}

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

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")
}

我们运行程序,点击按钮拍照,可以发现提示拍照成功
在这里插入图片描述
我们打开文件管理器,在/sdcard/Android/data/包名/files/image文件夹下,可以看到这张图片
但是我们发现这张照片的方向是不对的

在这里插入图片描述

4. 修正图片方向

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

首先添加OrientationLiveData这个LiveData

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}}
}

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

// 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")})
}

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

// 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 == 270 && mirrored -> ExifInterface.ORIENTATION_TRANSVERSErotationDegrees == 90 && !mirrored -> ExifInterface.ORIENTATION_ROTATE_90rotationDegrees == 90 && mirrored -> ExifInterface.ORIENTATION_TRANSPOSErotationDegrees == 270 && mirrored -> ExifInterface.ORIENTATION_ROTATE_270rotationDegrees == 270 && !mirrored -> ExifInterface.ORIENTATION_TRANSVERSEelse -> ExifInterface.ORIENTATION_UNDEFINED
}

再来运行程序,看到图片的方向就正常了
在这里插入图片描述

5. 销毁相机

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

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()
}

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

6. 其他

6.1 本文源码下载

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

6.2 Android Camera2 系列

更多Camera2相关文章,请看
十分钟实现 Android Camera2 相机预览_氦客的博客-CSDN博客
十分钟实现 Android Camera2 相机拍照_氦客的博客-CSDN博客
十分钟实现 Android Camera2 视频录制_氦客的博客-CSDN博客

6.3 Android 相机相关文章

Android 使用CameraX实现预览/拍照/录制视频/图片分析/对焦/缩放/切换摄像头等操作_氦客的博客-CSDN博客
Android 从零开发一个简易的相机App_android开发简易app_氦客的博客-CSDN博客


http://www.ppmy.cn/news/624145.html

相关文章

shell脚本检测进程的CPU内存占用率

使用方法&#xff1a; 把xxx替换为自己进程的名字&#xff0c;然后直接运行该脚本即可在当前目录下产生一个叫做memory_info.txt的文件&#xff0c;记录进程的CPU内存占用率信息。可以用来查看自己进程对系统资源的消耗情况。 #!/bin/bashprocess"xxx" output_file…

Qt动态多级导航菜单(三)

前文链接&#xff1a;Qt动态多级导航菜单&#xff08;二&#xff09; 本次更新&#xff0c;主要将原导航项NavItem抽象为基类NavItemBase, 并派生出带有图标样式的动态多级导航菜单。至此&#xff0c;关于想要增加自定义的导航菜单样式&#xff0c;只需要从NavItemBase类派生即…

《小王子》

小王子见到一只狐狸&#xff0c;想跟狐狸交往&#xff1b; 狐狸说我不能跟你玩耍&#xff0c; 因为你还没有驯服我&#xff0c; 还没有和我建立联系。 狐狸说对我而言&#xff0c; 你还只是一个小男孩&#xff0c; 和其他千万个小男孩一样&#xff0c; 我不需要你&#xff0c;你…

奶爸日记15 - 王子公主

在爸爸的关注下&#xff0c;娃娃自己拼图的小王子和小公主

小王子的链表

单链表 #include <bits/stdc.h> using namespace std;struct node{node* r;int x; }; int main() {int n;cin>>n;node* root new node;node* now root;for(int i 1 ; i < 10;i){node* temp new node;temp -> x i;temp -> r NULL;now -> r temp…

R的铁头王子

摸索着学R语言 批量读取文件夹中所有次级次次级文件&#xff0c;筛选并复制特定类型文件到新文件夹中 用了sapply()和file.copy()两种方法尝试,最后用sapply完成了。 file.copy() #获得该目录下的文件夹名称 list.files(path "D:/Tryone/T", recursive T) #复…

王子的烦恼(c++)

肝&#xff0c;加油&#xff01; 题目描述 勇敢的王子终于披荆斩棘的来到了关押睡美人的城堡&#xff0c;正当他准备把睡美人救出来的时候&#xff0c;竟悲催的发现——城堡大门竟然上锁了&#xff01; 因为时代在进步&#xff0c;城堡的锁也换上了八位的密码锁&#xff0c;王…

蓝桥杯备赛题 <小王子单链表>

题目描述 小王子有一天迷上了排队的游戏&#xff0c;桌子上有标号为 1-101−10 的 1010 个玩具&#xff0c;现在小王子将他们排成一列&#xff0c;可小王子还是太小了&#xff0c;他不确定他到底想把那个玩具摆在哪里&#xff0c;直到最后才能排成一条直线&#xff0c;求玩具的…