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();
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
相机工具类页面:
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;
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
HarmonyOS
赞
收藏 0
回答 1
相关问题
自定义相机预览,切回后台再切换回来预览黑屏
2442浏览 • 1回复 待解决
HarmonyOS 自定义相机预览问题
969浏览 • 1回复 待解决
HarmonyOS 自定义相机预览拉伸问题
864浏览 • 1回复 待解决
HarmonyOS 自定义相机demo
1586浏览 • 1回复 待解决
HarmonyOS 如何自定义相机
870浏览 • 1回复 待解决
HarmonyOS 自定义相机功能
1000浏览 • 1回复 待解决
HarmonyOS 自定义相机演示demo
1028浏览 • 1回复 待解决
HarmonyOS 关于自定义相机功能
982浏览 • 1回复 待解决
HarmonyOS 如何自定义相机背景
853浏览 • 1回复 待解决
HarmonyOS 自定义弹窗 (CustomDialog) 跳转再返回后消失
1180浏览 • 1回复 待解决
HarmonyOS ScanKit自定义界面扫码,相机流无法预览
874浏览 • 1回复 待解决
HarmonyOS 使用自定义相机左边有间距
775浏览 • 1回复 待解决
HarmonyOS 自定义相机拍照不成功
1120浏览 • 1回复 待解决
HarmonyOS 自定义相机如何实现点击屏幕聚焦
949浏览 • 1回复 待解决
HarmonyOS 自定义相机拍照后数据展示
1796浏览 • 1回复 待解决
能够提供HarmonyOS自定义相机案例吗?
1137浏览 • 1回复 待解决
HarmonyOS 自定义相机拍照如何判断预览画面中是否有人脸
610浏览 • 1回复 待解决
HarmonyOS 自定义相机前置摄像头变形
1144浏览 • 1回复 待解决
HarmonyOS PDF预览界面自定义
688浏览 • 1回复 待解决
【求助】自定义相机Camera2焦距异常
8977浏览 • 1回复 待解决
HarmonyOS 自定义相机拍照后的图片位置略有偏移
776浏览 • 1回复 待解决
HarmonyOS 对于相机开发,是否有对应自定义组件,比如相机框,身份证框
1223浏览 • 1回复 待解决
HarmonyOS 自定义弹窗怎么设置自定义动画?
1137浏览 • 1回复 待解决
HarmonyOS 自定义相机怎么实现全屏预览和产出的视频、图片的边界一致?
1263浏览 • 1回复 待解决
参考以下demo: