camera 相机回调后的数据如何存储为图片?
获取 Camera 实时帧数据frame.data
const listener = context.onCameraFrame((frame) => { console.log(frame.data instanceof ArrayBuffer, frame.width, frame.height)
|
这个frame.data类型是ArrayBuffer
data ArrayBuffer 图像像素点数据,一维数组,每四项表示一个像素点的 rgba |
如何将这个数据存储成一个图片?
尝试使用下面的java代码解析无法打开
Path path = Paths.get(fileDest); Files.write(path, bFile); |
3 回复
在后台用java保存了图片
public static void readRawImage( byte [] bgraPixelData, int width, int height, String path) throws IOException { //4通道 int samplesPerPixel = 4 ; // rgba order int [] bandOffsets = { 0 , 1 , 2 , 3 }; //byte[] bgraPixelData = new byte[width * height * samplesPerPixel]; DataBuffer buffer = new DataBufferByte(bgraPixelData, bgraPixelData.length); WritableRaster raster = Raster.createInterleavedRaster(buffer, width, height, samplesPerPixel * width, samplesPerPixel, bandOffsets, null ); ColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), true , false , Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE); BufferedImage image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null ); ImageIO.write(image, "PNG" , new File(path)); } |
利用小程序前端即可实现。
利用wxAPI将arraybuffer转为base64:https://developers.weixin.qq.com/miniprogram/dev/api/base/wx.arrayBufferToBase64.html
利用
wx.getFileSystemManager().writeFileSync()将base64存为本地文件即可