HarmonyOS 自定义相机,跳转至图库应用后再返回,自定义相机预览黑屏
自定义相机,跳转至图库应用后再返回,自定义相机预览黑屏,或者从后台再返回自定义相机页面的时候,也会出现预览黑屏的问题。
page页面:
async XComponentInit() {
this.XComponentController.setXComponentSurfaceSize({ surfaceWidth: 240, surfaceHeight: 320 });
this.surfaceId = this.XComponentController.getXComponentSurfaceId();
await this.camera.initCamera(this.surfaceId);
}
aboutToAppear(): void {
this.XComponentInit();
}
onPageShow() {
this.XComponentInit();
}
onPageHide() {
this.camera.releaseCamera();
}
async aboutToDisappear() {
await this.camera.releaseCamera();
}
相机工具类页面:
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { fileIo } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { camera } from '@kit.CameraKit';
import { common } from '@kit.AbilityKit';
import { image } from '@kit.ImageKit';
import { buffer } from '@kit.ArkTS';
export class CameraPhotoUtils {
private cameraManager?: camera.CameraManager
private cameraInput?: camera.CameraInput
public previewOutput?: camera.PreviewOutput
public photoOutput?: camera.PhotoOutput
private photoSession?: camera.PhotoSession
private receiver: image.ImageReceiver | undefined = undefined;
base64Src: string = '';
private pixelMap?: image.PixelMap
private photoUri: string = ''
async initCamera(surfaceId: string): Promise<void> {
this.cameraManager = camera.getCameraManager(getContext(this) as common.UIAbilityContext);
let cameraArray: Array<camera.CameraDevice> = this.cameraManager.getSupportedCameras();
let cameraDevice = cameraArray[0];
this.cameraInput = this.cameraManager.createCameraInput(cameraDevice);
await this.cameraInput.open();
let cameraOutputCap: camera.CameraOutputCapability =
this.cameraManager!.getSupportedOutputCapability(cameraDevice, camera.SceneMode.NORMAL_PHOTO);
let previewProfilesArray: Array<camera.Profile> = cameraOutputCap.previewProfiles;
let photoProfilesArray: Array<camera.Profile> = cameraOutputCap.photoProfiles;
this.previewOutput = this.cameraManager!.createPreviewOutput(previewProfilesArray[5], surfaceId);
let size: image.Size = {
height: 2592, width: 1200
};
this.receiver = image.createImageReceiver(size, image.ImageFormat.JPEG, 8);
this.photoOutput = this.cameraManager!.createPhotoOutput(photoProfilesArray[5]);
this.photoOutput.on('photoAvailable', (errCode: BusinessError, photo: camera.Photo): void => {
let imageObj = photo.main;
imageObj.getComponent(image.ComponentType.JPEG, async (errCode: BusinessError, component: image.Component) => {
if (errCode || component === undefined) {
return;
}
let mBuffer: ArrayBuffer;
mBuffer = component.byteBuffer;
console.info(JSON.stringify(mBuffer));
await this.savePicture(mBuffer);
})
imageObj.release();
})
this.photoSession = this.cameraManager!.createSession(camera.SceneMode.NORMAL_PHOTO);
this.photoSession.beginConfig();
this.photoSession.addInput(this.cameraInput);
this.photoSession.addOutput(this.previewOutput);
this.photoSession.addOutput(this.photoOutput);
await this.photoSession.commitConfig();
await this.photoSession.start();
this.photoSession.on('error', (error: BusinessError) => {
console.error(`Photo session error code: ${error.code}`);
});
}
async takePicture() {
console.log('takePicture');
this.photoOutput!.capture();
}
async savePicture(mBuffer: ArrayBuffer): Promise<void> {
let photoHelper: photoAccessHelper.PhotoAccessHelper =
photoAccessHelper.getPhotoAccessHelper(getContext(this) as common.UIAbilityContext);
let options: photoAccessHelper.CreateOptions = {
title: Date.now().toString()
};
//createAsset的调用需要ohos.permission.READ_IMAGEVIDEO和ohos.permission.WRITE_IMAGEVIDEO的权限
this.photoUri = await photoHelper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'jpg', options);
console.info(this.photoUri);
// 创建图片资源
const imageSource: image.ImageSource = image.createImageSource(mBuffer);
let decodingOptions: image.DecodingOptions = {
editable: true,
// desiredPixelFormat: 3,
}
// 创建pixelMap
this.pixelMap = await imageSource.createPixelMap(decodingOptions)
this.pixelMap.crop({ x: 32, y: 400, size: { height: 200, width: 375 } });
let file: fileIo.File = fileIo.openSync(this.photoUri, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
// await fileIo.write(file.fd, mBuffer);
// pixelMap 转 base64
const imagePackerApi = image.createImagePacker();
let packOpts: image.PackingOption = { format: "image/jpeg", quality: 100 };
imagePackerApi.packing(this.pixelMap, packOpts).then((data: ArrayBuffer) => {
// data 为打包获取到的文件流,写入文件保存即可得到一张图片
if (data) {
let buf: buffer.Buffer = buffer.from(data);
this.base64Src = 'data:image/jpeg;base64,' + buf.toString('base64', 0, buf.length);
console.info('base64Src: ' + this.base64Src);
console.info('Succeeded in packing the image.');
}
}).catch((error: BusinessError) => {
console.error('Failed to pack the image. And the error is: ' + error);
})
this.pixelMap.release()
imagePackerApi.release()
fileIo.closeSync(file);
}
async releaseCamera(): Promise<void> {
if (this.cameraInput) {
await this.cameraInput.close();
}
if (this.previewOutput) {
await this.previewOutput.release();
}
if (this.photoOutput) {
await this.photoOutput.release()
}
if (this.photoSession) {
await this.photoSession.release();
this.photoSession = undefined;
}
}
}
HarmonyOS
赞
收藏 0
回答 1
待解决
相关问题
自定义相机预览,切回后台再切换回来预览黑屏
1895浏览 • 1回复 待解决
HarmonyOS 自定义相机预览问题
38浏览 • 1回复 待解决
HarmonyOS 自定义相机demo
354浏览 • 1回复 待解决
HarmonyOS 如何自定义相机
29浏览 • 1回复 待解决
HarmonyOS 如何自定义相机背景
21浏览 • 1回复 待解决
HarmonyOS 自定义相机演示demo
246浏览 • 1回复 待解决
HarmonyOS 关于自定义相机功能
28浏览 • 1回复 待解决
HarmonyOS 自定义弹窗 (CustomDialog) 跳转再返回后消失
30浏览 • 1回复 待解决
HarmonyOS ScanKit自定义界面扫码,相机流无法预览
82浏览 • 1回复 待解决
HarmonyOS 自定义相机拍照不成功
19浏览 • 1回复 待解决
HarmonyOS 使用自定义相机左边有间距
29浏览 • 1回复 待解决
能够提供HarmonyOS自定义相机案例吗?
346浏览 • 1回复 待解决
HarmonyOS 自定义相机拍照后数据展示
818浏览 • 1回复 待解决
HarmonyOS 自定义相机如何实现点击屏幕聚焦
90浏览 • 1回复 待解决
HarmonyOS 自定义相机前置摄像头变形
38浏览 • 1回复 待解决
【求助】自定义相机Camera2焦距异常
8013浏览 • 1回复 待解决
HarmonyOS PDF预览界面自定义
48浏览 • 1回复 待解决
HarmonyOS 自定义相机拍照后的图片位置略有偏移
28浏览 • 1回复 待解决
HarmonyOS 自定义CustomDialog 跳转问题
15浏览 • 1回复 待解决
HarmonyOS 对于相机开发,是否有对应自定义组件,比如相机框,身份证框
444浏览 • 1回复 待解决
HarmonyOS 自定义相机拍完照后保存图片相关问题
43浏览 • 1回复 待解决
自定义弹窗自定义转场动画
1179浏览 • 1回复 待解决
HarmonyOS 自定义相机怎么实现全屏预览和产出的视频、图片的边界一致?
39浏览 • 1回复 待解决
HarmonyOS 自定义相机在不同类型设备上需要适配吗?
21浏览 • 1回复 待解决
参考以下demo: