PhotoOutput获取拍照图片时,无法设置分辨率,这个问题,可以设置分辨率,我们这边设置的是相机支持的分辨率,PhotoOutput中的profile属性不能自定义,只能通过接口获取,参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-camera-V5\#getsupportedoutputcapability11;第二个问题,开发者输出出来的图片无法预览,是因为开发者的带直接写进文件的是yuv数据,请参考下面的代码,生成yuv格式的图片:async yuv(receiver: image.ImageReceiver): Promise<void> { receiver.on('imageArrival', () => { console.error("imageArrival callback"); receiver.readNextImage((err, nextImage: image.Image) => { let a = nextImage.format nextImage.getComponent(image.ComponentType.JPEG, (err: BusinessError, imgComponent: image.Component) => { if (err || imgComponent === undefined) { // CLog.error('getComponent failed'); } if (imgComponent) { let path: string = getContext().filesDir + "/image1.yuv"; if (fileIo.accessSync(path)) { fileIo.rmdirSync(path) } let file = fileIo.openSync(path, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); let opt: WriteOptions = { // 多出2048字节数据 length: imgComponent.byteBuffer.byteLength } fileIo.writeSync(file.fd, imgComponent.byteBuffer, opt) fileIo.close(file) } else { // CLog.error('byteBuffer is null'); } }) }) }) }如果想获取JPEG,请参考这个demo:async onImageArrival(receiver: image.ImageReceiver): Promise<void> { receiver.on('imageArrival', () => { console.error("imageArrival callback"); receiver.readNextImage((err, nextImage: image.Image) => { nextImage.getComponent(image.ComponentType.JPEG, async (err, imgComponent: image.Component) => { if (err || imgComponent === undefined) { return; } if (imgComponent.byteBuffer as ArrayBuffer) { let sourceOptions: image.SourceOptions = { sourceDensity: 120, sourcePixelFormat: 8, // NV21 sourceSize: { height: this.previewProfilesObj2!.size.height, width: this.previewProfilesObj2!.size.width }, } let imageResource = image.createImageSource(imgComponent.byteBuffer, sourceOptions); // // 设置参数 let decodingOptions: image.DecodingOptions = { sampleSize:1, // 缩略图采样大小 editable: true, // 是否可编辑 desiredSize:{ width:500, height:500}, // 期望输出大小 rotate: 270, // 旋转角度 desiredPixelFormat:8, // 解码的像素格式 desiredRegion: { size: { height:500, width:500 }, x: 0, y: 0 }, // 解码的区域 index:0 // 图片序号 }; imageResource.createPixelMap(decodingOptions).then((res)=>{ this.imgUrl = res; }); } else { return; } nextImage.release(); }) }) }) }
PhotoOutput获取拍照图片时,无法设置分辨率,这个问题,可以设置分辨率,我们这边设置的是相机支持的分辨率,PhotoOutput中的profile属性不能自定义,只能通过接口获取,参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-camera-V5\#getsupportedoutputcapability11;
第二个问题,开发者输出出来的图片无法预览,是因为开发者的带直接写进文件的是yuv数据,请参考下面的代码,生成yuv格式的图片:
如果想获取JPEG,请参考这个demo: