当使用CameraKit进行拍照,然后通过如下代码获取图片buffer,但是通过photo: camera.Photo获取的图片宽高为[25165824,1]。
photoOutput.on('photoAvailable', (errCode: BusinessError, photo: camera.Photo): void => {
let imageObj = photo.main
imageObj.getComponent(image.ComponentType.JPEG, async (errCode: BusinessError, component: image.Component): Promise<void> => {
let buffer: ArrayBuffer = component.byteBuffer
this.savePicture(buffer, imageObj)
})
})
目前按照4:3拍照比例进行拍照,当切换至1:1之后,只进行遮挡,然后通过裁剪进行解决,下列代码是将拍照获取的buffer转为pixelmap,然后通过pixelmap.crop进行裁切,裁切完成之后,在转为buffer写入相册。
/*
* PixelMap转Buffer
* */
PixelMapToBuffer(pixelMap: image.PixelMap, width: number, height: number): ArrayBuffer{
//缓存区大小 = width * height * 4
let bufferLength = width * height * 4
const buffer: ArrayBuffer = new ArrayBuffer(bufferLength)
pixelMap.readPixelsToBufferSync(buffer)
return buffer
}
/*
* Buffer转PixelMap
* */
async BufferToPixelMap(buffer: ArrayBuffer, width: number, height: number): Promise<image.PixelMap>{
let sourceOptions: image.SourceOptions = {
sourceDensity: 0, // 在不确定当前密度时传0
sourcePixelFormat: image.PixelMapFormat.RGBA_8888,
sourceSize: {width: width, height: height}
}
let imageSource: image.ImageSource = image.createImageSource(buffer, sourceOptions)
let opts: image.InitializationOptions = {
editable: false,
pixelFormat: image.PixelMapFormat.RGBA_8888,
size: {width: width, height: height}
}
return await imageSource.createPixelMap(opts)
}
try {
//6144 8192
let pixelMap = await this.pixelMapBufferConvertUtil.BufferToPixelMap(buffer, 6144, 8192)
if (!pixelMap) {
LoggerJoy.error(`CameraPage--> occur error when crop photo!`)
return undefined
}
LoggerJoy.info(`CameraPage--> the image size is [${size.width},${size.height}]`)//25165824,1
pixelMap.cropSync({x: 0, y: 0, size: {width: 6144, height: 8192}})
return this.pixelMapBufferConvertUtil.PixelMapToBuffer(pixelMap, 6144, 8192)
} catch (error) {
let err = error as BusinessError
LoggerJoy.error(`CameraPage--> occur error when crop photo, the error code: ${err.code}, error message: ${err.message}`)
return undefined
}
目前所遇到的问题是拍照获取的图片宽高为[25165824,1],裁切之后的图片在相册无法预览。
请参考以下示例: