中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
这里目前实现类视频流数据的获取,获取到buffer,目前想了解的是,这个视频流的buffer是什么格式的 如何保存成图片呢
API10 Devstudio 4.1 华为mate 60pro
微信扫码分享
createImageReceiver(): image.ImageReceiver { let receiver: image.ImageReceiver = image.createImageReceiver(cameraWidth, cameraHeight, 2000, 8); receiver.on(‘imageArrival’, () => { receiver.readNextImage((err: BusinessError, nextImage: image.Image) => { if (err || nextImage === undefined) { return; } nextImage.getComponent(image.ComponentType.JPEG, async (err: BusinessError, imgComponent: image.Component) => { if (err || imgComponent === undefined) { return; } if (imgComponent.byteBuffer as ArrayBuffer) { let sourceOptions : image.SourceOptions = { sourceDensity: 120, sourcePixelFormat: 8, // NV21 sourceSize: { height: previewProfilesObj3.size.height, width: previewProfilesObj3.size.width } } let imageResource=image.createImageSource(imgComponent.byteBuffer,sourceOptions) let decodingOptions: image.DecodingOptions = { editable: true, desiredPixelFormat: 3, } this.pixma=await imageResource.createPixelMap(decodingOptions) const imagePackerApi = image.createImagePacker(); let packOpts: image.PackingOption = { format: “image/jpeg”, quality: 98 }; const context: Context = getContext(this); const path1: string = context.cacheDir + “/pixel_map86695.jpg”; imagePackerApi.packing(imageResource, packOpts).then((data: ArrayBuffer) => { // data 为打包获取到的文件流,写入文件保存即可得到一张图片 if (data) { let buf: buffer.Buffer = buffer.from(data ); let base64Url: string = buf.toString(‘base64url’, 0, buf.length); console.log(‘66666’+JSON.stringify(base64Url)) let file = fs.openSync(path1, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); fs.write(file.fd, data).then(async (writeLen) => { console.info("write data to file succeed and size is:" + writeLen); fs.closeSync(file); }).catch((err: BusinessError) => { console.info("write data to file failed with error message: " + err.message + ", error code: " + err.code); }); } }).catch((error: BusinessError) => { console.error('Failed to pack the image. And the error is: ’ + error); }) this.pixelmap=await imageResource.createPixelMap(); } else { return; } }) }) }) return receiver; }