HarmonyOS鸿蒙Next中相机native方案如何控制采集到的数据的方向?
HarmonyOS鸿蒙Next中相机native方案如何控制采集到的数据的方向?
当前我们采用的是native方案去采集摄像头画面,是通过将OH_NativeBuffer对应的多通道ION内存映射到进程空间去获取对应的yuv数据,用的是OH_NativeBuffer_Map接口,但是我们发现采集到的数据不能根据屏幕方向旋转而变化,OH_PreviewOutput_SetPreviewRotation是新增的接口,但是设置了也没有效果,想问下这块有最佳实践吗?
Camera_ErrorCode NDKCamera::CreatePreviewOutput(void) {
profile_ = cameraOutputCapability_->previewProfiles[0];
if (profile_ == nullptr) {
OH_LOG_ERROR(LOG_APP, "Get previewProfiles failed.");
return CAMERA_INVALID_ARGUMENT;
}
ret_ = OH_CameraManager_CreatePreviewOutput(cameraManager_, profile_, previewSurfaceId_, &previewOutput_);
if (previewSurfaceId_ == nullptr || previewOutput_ == nullptr || ret_ != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "CreatePreviewOutput failed.");
return CAMERA_INVALID_ARGUMENT;
}
OH_PreviewOutput_SetPreviewRotation(previewOutput_, IAMGE_ROTATION_90, false);
return ret_;
}
更多关于HarmonyOS鸿蒙Next中相机native方案如何控制采集到的数据的方向?的实战教程也可以访问 https://www.itying.com/category-93-b0.html
"旋转角度的变化的话,你试试这个方法能否满足您的需求,
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/camera-rotation-angle-adaptation-V5
这边引入项目之后选择运行的模块是xwindow,然后run您的demo,直接进入的是app的管理页面,通知里面的运行通知和置顶显示通知都已经全部开了,重新进入还是进入到了app的通知页面
如果不符合的话,麻烦说下你的ide版本和真机版本~~
麻烦你看下这个,通过NDK接口调用相机,可以参考demo:
https://gitee.com/openharmony/applications_app_samples/tree/master/code/BasicFeature/Media/Camera
很抱歉没有该OH_PreviewOutput_SetPreviewRotation的最佳实践demo,但是下面提供的demo并没有出现你说的旋转变化的情况,作为代替的方案
https://gitee.com/harmonyos_samples/camera-data-collection
请谅解~~
这边在官方文档和其他一些途径并没有查到这个OH_PreviewOutput_SetPreviewRotation接口,唯一查到的地方就是之前提供给你您的参考链接demo中,方便提供下这个你参考这个接口的文档吗,方便这边参考下,感谢
/**
* @brief Sets the preview rotation angle.
*
* @param previewOutput the @link Camera_PreviewOutput} instance which used to set the preview rotation angle.
* @param previewRotation the @link Camera_ImageRotation} of preview display rotation angle.
* @param isDisplayLocked TRUE means the display is locked.
* @return @link #CAMERA_OK} if the method call succeeds.
* @link #CAMERA_INVALID_ARGUMENT} if parameter missing or parameter type incorrect.
* @link #CAMERA_SERVICE_FATAL_ERROR} if camera service fatal error.
* @since 12
*/
Camera_ErrorCode OH_PreviewOutput_SetPreviewRotation(Camera_PreviewOutput * previewOutput,
Camera_ImageRotation previewRotation, bool isDisplayLocked);
麻烦您试试先get,然后再set试试,先调用OH_PreviewOutput_GetPreviewRotation,然后再OH_PreviewOutput_SetPreviewRotation
/**
* @brief Gets the preview rotation angle.
*
* @param previewOutput the @link Camera_PreviewOutput} instance which used to get the preview rotation angle.
* @param displayRotation the current display rotation angle.
* @param imageRotation the @link Camera_ImageRotation} result of preview rotation angle.
* @return @link #CAMERA_OK} if the method call succeeds.
* @link #CAMERA_INVALID_ARGUMENT} if parameter missing or parameter type incorrect.
* @link #CAMERA_SERVICE_FATAL_ERROR} if camera service fatal error.
* @since 12
*/
Camera_ErrorCode OH_PreviewOutput_GetPreviewRotation(Camera_PreviewOutput * previewOutput, int displayRotation,
Camera_ImageRotation * imageRotation);
/**
* @brief Sets the preview rotation angle.
*
* @param previewOutput the @link Camera_PreviewOutput} instance which used to set the preview rotation angle.
* @param previewRotation the @link Camera_ImageRotation} of preview display rotation angle.
* @param isDisplayLocked TRUE means the display is locked.
* @return @link #CAMERA_OK} if the method call succeeds.
* @link #CAMERA_INVALID_ARGUMENT} if parameter missing or parameter type incorrect.
* @link #CAMERA_SERVICE_FATAL_ERROR} if camera service fatal error.
* @since 12
*/
Camera_ErrorCode OH_PreviewOutput_SetPreviewRotation(Camera_PreviewOutput * previewOutput,
Camera_ImageRotation previewRotation, bool isDisplayLocked);
OH_PreviewOutput_GetPreviewRotation这个接口调用,需要previewoutput的对象,在commitconfig之后,第二个参数是要根据获取屏幕旋转角度的接口获取到的值,第三个是出参就是结果,然后用出参传入set接口,OH_PreviewOutput_SetPreviewRotation出参设置到第二个参数里,第三个参数默认填false
麻烦按照这个方法试试哈
更多关于HarmonyOS鸿蒙Next中相机native方案如何控制采集到的数据的方向?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,相机native方案通过CameraKit API控制采集到的数据方向。CameraKit提供了CameraCaptureSession和CaptureRequest类来管理相机操作。通过设置CaptureRequest的JPEG_ORIENTATION参数,可以控制采集到的图像数据的方向。该参数接受一个整数值,表示图像旋转的角度,通常为0、90、180或270度。具体实现如下:
- 创建
CameraCaptureSession并配置相机设备。 - 通过
CaptureRequest.Builder设置JPEG_ORIENTATION参数。 - 提交
CaptureRequest以应用方向设置。
确保在设置方向时考虑设备的物理方向,以便正确调整图像数据。
在HarmonyOS鸿蒙Next中,相机Native方案通过CameraDevice和CameraOutputCapability接口获取设备支持的输出格式和方向信息。使用CameraCaptureSession配置捕获会话时,通过setDisplayOrientation方法设置预览方向。对于采集到的数据,可以通过Image对象的getRotation方法获取图像方向,并使用ImageReader的setOnImageAvailableListener监听图像数据,结合Matrix进行旋转处理,确保数据方向正确。

