AndroidR上展锐平台实现长按拍照键连拍的功能

2024-05-24 02:08

本文主要是介绍AndroidR上展锐平台实现长按拍照键连拍的功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

某客户定制需求:长按拍照键实现连拍功能。
我们先来跟进下单拍的流程,连拍与单拍流程重叠度很高,只是连拍在单拍的照片callback回来之后,在立即下发下一次的拍照请求。
View的click流程就不说了,点击拍照键后,点击事件会回调到PhotoModule的onShutterButtonClick方法中。
在这个方法中会做拍照前的逻辑判断,比如当前对否设置了拍照倒计时,如果有的话,就转到倒计时的逻辑中,延时拍照。我们这里不考虑这些设置,后面就是调用FocusOverlayManager的 focusAndCapture()。这个方法我们应该挺熟悉的,我们之前有篇文章专门介绍预览聚焦的流程,这里在拍照之前也去确认了当前是否已经是聚焦OK的状态,否则我们会拍出模糊的照片。

FocusOverlayManager下的focusAndCapture方法

public void focusAndCapture(CameraCapabilities.FocusMode currentFocusMode) {if (!mInitialized) {return;}if(isAFLock && mState != STATE_FOCUSING){capture();return;}/*** SPRD:fix bug 473602 add for half-press @{if (!needAutoFocusCall(currentFocusMode)) {*/if (!needAutoFocusCall(currentFocusMode) && mState != STATE_FOCUSING) {/*** @}*/// Focus is not needed.Log.i(TAG, "Focus is not needed.");capture();} else if (mState == STATE_SUCCESS || mState == STATE_FAIL) {// Focus is done already.Log.i(TAG, "Focus is done already.");capture();} else if (mState == STATE_FOCUSING) {// Still focusing and will not trigger snap upon finish.Log.i(TAG, "till focusing and will not trigger snap upon finish.");mState = STATE_FOCUSING_SNAP_ON_FINISH;} else if (mState == STATE_IDLE) {autoFocusAndCapture();}}

我们假定当前是已经在预览时聚焦OK的状态,一般情况下在预览阶段就已经聚焦好了,那么会在调用capture方法。

private void capture() {if (mListener.capture()) {if (!mFocusRing.isAEAFDraging()) {mState = STATE_IDLE;}mHandler.removeMessages(RESET_TOUCH_FOCUS);}
}

capture方法啥也没干,又把流程转到mListener(PhotoModule)中去了。

PhotoModule的capture方法前半部分做一些mCameraSettings的更新,最后就下发takePicture的流程

@Override
public boolean capture() {Log.i(TAG, "capture");mBurstCaptureCountOnCanceled = -1;//SPRD:fix bug1085123if(!mBurstWorking && mIsContinousCaptureFinish){Log.i(TAG,"capture cancel");mAppController.getCameraAppUI().setBottomPanelLeftRightClickable(true);mAppController.getCameraAppUI().setSwipeEnabled(true);mBurstNotShowShutterButton = false;return false;}// If we are already in the middle of taking a snapshot or the image// save request is full then ignore.if (mCameraDevice == null || mCameraState == SNAPSHOT_IN_PROGRESS|| mCameraState == SWITCHING_CAMERA) {return false;}setCameraState(SNAPSHOT_IN_PROGRESS);mCaptureStartTime = System.currentTimeMillis();mPostViewPictureCallbackTime = 0;mJpegImageData = null;/* SPRD: add hide shutter animation after capture when HDR is onfinal boolean animateBefore = (mSceneMode == CameraCapabilities.SceneMode.HDR);if (animateBefore) {animateAfterShutter();}*/updateFilterType();/* SPRD: fix bug672841 add for cancel burst when focusing state, burst can not stop*/if (!mIsImageCaptureIntent) {updateParametersBurstCount();mContinuousCaptureCount = getContinuousCount();}/* @} */Location loc = null;if (mIsImageCaptureIntent && !mActivity.getThirdLocationPermission()) {Log.i(TAG,"third app call camera, has not location premission");CameraUtil.setGpsParameters(mCameraSettings, null);} else {loc = mActivity.getLocationManager().getCurrentLocation();Log.i(TAG,"location info:"+loc);CameraUtil.setGpsParameters(mCameraSettings, loc);}boolean isHasEnteredBeauty = mDataModule.getBoolean(Keys.KEY_CAMERA_BEAUTY_ENTERED);/* SPRD:fix bug 823475 clear ae corrdinate before capture when after taf @ */if (mFocusManager != null) {mFocusManager.updateFocusState();setMeteringAreasIfSupported();}/* @} */if(isAutoHdr() && isHdrScene){Log.e(TAG,"auto hdr detect scene is hdr so change the scene to hdr");mCameraSettings.setSceneMode(CameraCapabilities.SceneMode.HDR);}if (mSmileCapture) {mCameraSettings.setSmileCapture(1);mSmileCapture = false;}mCameraDevice.applySettings(mCameraSettings);// Set JPEG orientation. Even if screen UI is locked in portrait, camera// orientation should// still match device orientation (e.g., users should always get// landscape photos while// capturing by putting device in landscape.)Characteristics info = mActivity.getCameraProvider().getCharacteristics(mCameraId);int sensorOrientation = info.getSensorOrientation();int deviceOrientation = mAppController.getOrientationManager().getDeviceOrientation().getDegrees();boolean isFrontCamera = info.isFacingFront();mJpegRotation = CameraUtil.getImageRotation(sensorOrientation,deviceOrientation, isFrontCamera);Log.i(TAG, " sensorOrientation = " + sensorOrientation+ " ,deviceOrientation = " + deviceOrientation+ " isFrontCamera = " + isFrontCamera);mCameraDevice.setJpegOrientation(mJpegRotation);Log.i(TAG, "takePicture start!");isHdrPicture = isAutoHdr() && isHdrScene;if (mReceiver != null) {//mActivity.unregisterReceiver(mReceiver);mActivity.unRegisterMediaBroadcastReceiver();mReceiver = null;}mIsHdrPicture = true;mIsFirstCallback = true;mFirstHasStartCapture = false;mIsNormalHdrDone = false;//SPRD:fix bug784774if (inBurstMode()){doCaptureSpecialContinue();} else {doCaptureSpecial();}//开始拍照啦mCameraDevice.takePicture(mHandler,/*** SPRD: fix bug462021 remove capture animation* * @{ new ShutterCallback(!animateBefore),*/new ShutterCallback(CameraUtil.isCaptureAnimatationEnable() && !isBurstCapture() && !isAudioCapture() &&!isCameraFrontFacing()),//SPRD:fix bug1154938/1137366/1162992/1201491/*** @}*/mRawPictureCallback, mPostViewPictureCallback, new JpegPictureCallback(loc));/*** SPRD: fix bug 473462 add for burst capture* mNamedImages.nameNewImage(mCaptureStartTime);*/mFaceDetectionStarted = false;return true;
}

我们关注最后的 mCameraDevice.takePicture(…),重点来了,该方法是在AndroidCamera2AgentImpl中的 AndroidCamera2ProxyImpl 实现的。
这个方法比较长,分为两个部分,
1, 构建一个 CaptureAvailableListener 类型的 picListener 对象,作为照片数据的回调对象

// TODO: We never call raw or postview
final CaptureAvailableListener picListener =new CaptureAvailableListener() {
@Override
public void onCaptureStarted(CameraCaptureSession session, CaptureRequest request,long timestamp, long frameNumber) {// SPRDLog.i(TAG,"AppFw takePicture onCaptureStarted");if (shutter != null) {handler.post(new Runnable() {@Overridepublic void run() {/** SPRD @{* Original Code*if (mShutterSoundEnabled) {mNoisemaker.play(MediaActionSound.SHUTTER_CLICK);}*/shutter.onShutter(AndroidCamera2ProxyImpl.this);}});}
}@Override
public void onImageAvailable(ImageReader reader) {// 第一部分,处理Burst相关的if (mPicListener != null && !mBurstCaptureCanceled) {if (mBurstHasCaptureCount < mBurstMaxCaptureCount) {Log.i(TAG, "AppFw takePicture onImageAvailable mBurstHasCaptureCount = "+ mBurstHasCaptureCount);try {mDispatchThread.runJob(new Runnable() {@Overridepublic void run() {if (mBurstCaptureCanceled) {Log.i(TAG,"burst capture canceled");return;}mCameraState.waitForStates(~(AndroidCamera2StateHolder.CAMERA_PREVIEW_ACTIVE - 1));mBurstHasCaptureCount++;//SPRD:fix bug756490mCameraHandler.obtainMessage(CameraActions.CAPTURE_PHOTO, mPicListener).sendToTarget();}});} catch (RuntimeException ex) {mCameraAgent.getCameraExceptionHandler().onDispatchThreadException(ex);}}}Log.i(TAG,"AppFw takePicture onImageAvailable");//第二部分,处理上报的图片数据try (Image image = reader.acquireNextImage()) {if (!mNeedThumb && jpeg != null) {ByteBuffer buffer = image.getPlanes()[0].getBuffer();final byte[] pixels = new byte[buffer.remaining()];buffer.get(pixels);handler.post(new Runnable() {@Overridepublic void run() {jpeg.onPictureTaken(pixels, AndroidCamera2ProxyImpl.this);}});} else {onImageAvailableWithThumb(image, handler, jpeg);}}
}};

在该回调对象的 onImageAvailable 方法中,就是处理图片数据。onImageAvailable也分为两个部分。第一部分的if里面,我们看到Burst相关内容,没错,这就是处理连拍相关的逻辑的,我们先暂时略过,稍后会再回来讲这部分。
在第二部分中,我们看到一个很熟悉的方法onPictureTaken,这就是我们在PhotoModule中经常看到的图片数据的回调方法,在onPictureTaken中处理图片命名、保存到本地等逻辑。

2, CaptureAvailableListener 的回调对象我们看完了,在来看 takePicture 的第二部分。

try {mPicListener = picListener;mBurstMaxCaptureCount = mLastSettings.mBurstNumber;Log.i(TAG, "AppFw takePicture mBurstMaxCaptureCount now is " + mBurstMaxCaptureCount);if(takePictureRunnable == null) {Log.i(TAG, "AppFw takePicture new runJob");takePictureRunnable = new Runnable() {@Overridepublic void run() {Log.i(TAG, "AppFw takePicture runJob");if (mBurstMaxCaptureCount > 1) {mBurstHasCaptureCount = 0;// reset capture count, in case of didn't call// cancelBurstCapturemBurstHasCaptureCount++;mBurstCaptureCanceled = false;}// Wait until PREVIEW_ACTIVE or bettermCameraState.waitForStates(~(com.android.ex.camera2.portability.AndroidCamera2AgentImpl.AndroidCamera2StateHolder.CAMERA_PREVIEW_ACTIVE - 1));// SPRD : Add for bug 657472 Save normal hdr pictureif(mLastSettings.getNormalHdrModeEnable() == 1){mCameraHandler.obtainMessage(CameraActions.CAPTURE_HDR_PHOTO, mPicListener).sendToTarget();} else if (mNeedThumb) {mCameraHandler.obtainMessage(CameraActions.CAPTURE_PHOTO_WITH_THUMB, mPicListener).sendToTarget();} else if (mIsVideMode) {mCameraHandler.obtainMessage(CameraActions.CAPTURE_PHOTO_WITH_SNAP, mPicListener).sendToTarget();}else{mCameraHandler.obtainMessage(CameraActions.CAPTURE_PHOTO, mPicListener).sendToTarget();}/* @} */}};}mDispatchThread.runJob(takePictureRunnable);
} catch (RuntimeException ex) {takePictureRunnable = null;mCameraAgent.getCameraExceptionHandler().onDispatchThreadException(ex);
}

又看到熟悉的Hander发送消息,这次发送的是 CAPTURE_PHOTO ,看名称就知道 拍照 的消息。并且注意到我们前面的 CaptureAvailableListener picListener 作为handler消息的obj了。

接下来我们就看下,CAPTURE_PHOTO 消息的处理。

case CameraActions.CAPTURE_PHOTO: {final CaptureAvailableListener listener =(CaptureAvailableListener) msg.obj;if (mLegacyDevice || mIsYuvSensor || mCameraProxy.mLastSettings.mBurstNumber == 30 ||//SPRD: fix bug962989
mCameraProxy.mLastSettings.mBurstNumber == 20 ||(mPersistentSettings.matches(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_OFF)) ||((mCurrentAeState == CaptureResult.CONTROL_AE_STATE_CONVERGED || mCurrentAeState == CaptureResult.CONTROL_AE_STATE_FLASH_REQUIRED) &&//SPRD:fix bug1193947!mPersistentSettings.matches(CaptureRequest.CONTROL_AE_MODE,CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH) &&!mPersistentSettings.matches(CaptureRequest.FLASH_MODE,CaptureRequest.FLASH_MODE_SINGLE))){// SPRDLog.i(TAG,"AppFw:   CameraActions.CAPTURE_PHOTO, mLegacyDevice = " + mLegacyDevice + " mIsYuvSensor = " + mIsYuvSensor);mCaptureReader.setOnImageAvailableListener(listener, /*handler*/this);try {mSession.capture(mPersistentSettings.createRequest(mCamera,CameraDevice.TEMPLATE_STILL_CAPTURE,mCaptureReader.getSurface()),listener, /*handler*/this);} catch (CameraAccessException ex) {Log.e(TAG, "Unable to initiate immediate capture", ex);}} 

我们看到 if 的判断很复杂,但是我们最终还是进入到这个if条件里面了,并且我们前面讲的 CaptureAvailableListener listener 也从消息里面取出来了 (CaptureAvailableListener) msg.obj;

然后就是通过在创建会话时得到的mSession对象,去调用CameraCaptureSession的capture方法,并将CaptureAvailableListener 作为回调对象传下去。

到这里,我们的拍照流程算是讲完了。我们将上拍照流程总结为一个时序图
在这里插入图片描述

好了,拍照流程讲完了,下面我们来说下连拍的逻辑。

我们前面说过 CaptureAvailableListener picListener 这个回调,是用来接收拍照数据的,其中的onImageAvailable方法中有关于Burst的处理。我们再贴下代码

if (mPicListener != null && !mBurstCaptureCanceled) {if (mBurstHasCaptureCount < mBurstMaxCaptureCount) {Log.i(TAG, "AppFw takePicture onImageAvailable mBurstHasCaptureCount = "+ mBurstHasCaptureCount);try {mDispatchThread.runJob(new Runnable() {@Overridepublic void run() {if (mBurstCaptureCanceled) {Log.i(TAG,"burst capture canceled");return;}mCameraState.waitForStates(~(AndroidCamera2StateHolder.CAMERA_PREVIEW_ACTIVE - 1));mBurstHasCaptureCount++;//SPRD:fix bug756490mCameraHandler.obtainMessage(CameraActions.CAPTURE_PHOTO, mPicListener).sendToTarget();}});} catch (RuntimeException ex) {mCameraAgent.getCameraExceptionHandler().onDispatchThreadException(ex);}}
}

我们看到,如果这个if条件能满足,就会在发送一个 CAPTURE_PHOTO 消息去拍照。这其实就是连拍的逻辑,在前一张图片的数据callbakc回来之后,再去下发一个新的 CAPTURE_PHOTO 拍照请求,从而实现连拍。

那么,如何才能满足这个if条件呢?

我们看到有 mBurstCaptureCanceled 和 mBurstMaxCaptureCount 变量。如果满足连拍条件,mBurstCaptureCanceled = false;
在该类中搜索, mBurstCaptureCanceled 默认为true,只有如下位置会置为false。

 mBurstMaxCaptureCount = mLastSettings.mBurstNumber;if (mBurstMaxCaptureCount > 1) {mBurstHasCaptureCount = 0;// reset capture count, in case of didn't call// cancelBurstCapturemBurstHasCaptureCount++;mBurstCaptureCanceled = false;}

看来关键是 mLastSettings.mBurstNumber;的值了,在SprdCameraSettings中有唯一方法设置mBurstNumber的值

public void setBurstPicNum(int count) {mBurstNumber = count;
}

在全局搜一把 setBurstPicNum 方法的调用。
果然又来到了PhotoModule中

private void updateParametersBurstCount() {mCameraSettings.setBurstPicNum(!mBurstWorking ? 1 : MAX_BURST_COUNT);
}

如果 mBurstWorking = true,那么 setBurstPicNum传入的就是MAX_BURST_COUNT值了,而 mBurstWorking = true的条件之一便是当前是 inBurstMode()返回true。

public boolean inBurstMode() {return getModuleTpye() == DreamModule.AUTO_PHOTO_MODULE && mLongPressed; }

mLongPressed,长按拍照键条件。
所以只有长按拍照键,才会设置 mLastSettings.mBurstNumber 为 MAX_BURST_COUNT,在AndroidCamera2AgentImpl中才会设置 mBurstCaptureCanceled = false,才会在前一张图片数据callback之后立即下发下一次的 CAPTURE_PHOTO 拍照请求。

拍照和连拍的逻辑大部分都是一样的,只是连拍会设置SprdCameraSettings的mBurstNumber 值,在AndroidCamera2AgentImpl中会自动连续下发拍照请求。

这篇关于AndroidR上展锐平台实现长按拍照键连拍的功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/997066

相关文章

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

Java实现时间与字符串互相转换详解

《Java实现时间与字符串互相转换详解》这篇文章主要为大家详细介绍了Java中实现时间与字符串互相转换的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、日期格式化为字符串(一)使用预定义格式(二)自定义格式二、字符串解析为日期(一)解析ISO格式字符串(二)解析自定义

opencv图像处理之指纹验证的实现

《opencv图像处理之指纹验证的实现》本文主要介绍了opencv图像处理之指纹验证的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录一、简介二、具体案例实现1. 图像显示函数2. 指纹验证函数3. 主函数4、运行结果三、总结一、