HarmonyOS 鸿蒙Next视频流格式与保存方法咨询
HarmonyOS 鸿蒙Next视频流格式与保存方法咨询
咨询描述:
这里目前实现类视频流数据的获取,获取到buffer,目前想了解的是,这个视频流的buffer是什么格式的 如何保存成图片呢
咨询场景描述:
API10 Devstudio 4.1 华为mate 60pro
更多关于HarmonyOS 鸿蒙Next视频流格式与保存方法咨询的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
解决措施:
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))
<span class="hljs-keyword"><span class="hljs-keyword">let</span></span> file = fs.openSync(path1, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); fs.write(file.fd, data).then(async (writeLen) => { console.info(<span class="hljs-string"><span class="hljs-string">"write data to file succeed and size is:"</span></span> + 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; }
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
更多关于HarmonyOS 鸿蒙Next视频流格式与保存方法咨询的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
貌似没有效果
HarmonyOS 鸿蒙Next支持多种视频流格式,包括但不限于H.264和H.265编码的MP4视频文件。视频流数据可以通过Camera Kit等API获取,并传递到native层进行进一步处理,如压缩编码。编码后的视频文件可以保存到应用的沙箱路径或其他指定位置。关于具体的保存方法,建议参考HarmonyOS官方文档或API指南。如果问题依旧没法解决请加我微信,我的微信是itying888。
更多关于HarmonyOS 鸿蒙Next视频流格式与保存方法咨询的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html