|
|
@@ -22,7 +22,7 @@ import { BusinessError } from '@kit.BasicServicesKit';
|
|
|
import ResponseCode from '@ohos.net.http';
|
|
|
import { photoAccessHelper } from '@kit.MediaLibraryKit';
|
|
|
import fs from '@ohos.file.fs';
|
|
|
-import { FileUtil, MD5, PreferencesUtil } from '@pura/harmony-utils';
|
|
|
+import { Base64Util, FileUtil, LogUtil, MD5, PreferencesUtil } from '@pura/harmony-utils';
|
|
|
import MediaTable from './MediaTable';
|
|
|
|
|
|
const TAG = 'ImageUtils';
|
|
|
@@ -270,6 +270,72 @@ class ImageUtils {
|
|
|
|
|
|
|
|
|
}
|
|
|
+ public imagePathToPixelMap(imagePath: string): Promise<image.PixelMap> {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ try {
|
|
|
+
|
|
|
+ let file = fileIo.openSync(imagePath, fileIo.OpenMode.READ_ONLY | fileIo.OpenMode.CREATE)
|
|
|
+ let fd = file.fd;
|
|
|
+
|
|
|
+ let imageSource = image.createImageSource(fd);
|
|
|
+ let opts: image.DecodingOptions = { editable: false };
|
|
|
+
|
|
|
+ // 使用ImageSource创建PixelMap
|
|
|
+ imageSource.createPixelMap(opts).then(pixelMap => {
|
|
|
+ resolve(pixelMap);
|
|
|
+ }).catch((error:Error) => {
|
|
|
+ console.error(`onecold 创建PixelMap失败: ${error.message}`);
|
|
|
+ reject(error);
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ } catch (error) {
|
|
|
+ console.error(`处理异常: ${error.message}`);
|
|
|
+ reject(error);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * PixelMap转图片base64字符串
|
|
|
+ * @param pixelMap
|
|
|
+ * @param format 目标格式,默认png,当前只支持jpg、webp和png。当传入的格式与文件格式不匹配,可能会导致生成错误的Base64字符串。
|
|
|
+ * @returns
|
|
|
+ */
|
|
|
+ public async pixelMapToBase64StrBig(pixelMap: image.PixelMap|undefined, format: string = 'image/png'): Promise<string> {
|
|
|
+ if(pixelMap === undefined)
|
|
|
+ return ''
|
|
|
+ try {
|
|
|
+ let packOpts: image.PackingOption = { format: format, quality: 98 }
|
|
|
+ const arrayBuffer = await ImageUtils.packingFromPixelMap(pixelMap, packOpts);
|
|
|
+ let base64Str: string = Base64Util.encodeToStrSync(new Uint8Array(arrayBuffer));
|
|
|
+ let headStr = `data:${format};base64,`;
|
|
|
+ if (!base64Str.startsWith(headStr)) {
|
|
|
+ base64Str = headStr + base64Str
|
|
|
+ }
|
|
|
+ return base64Str;
|
|
|
+ } catch (err) {
|
|
|
+ let error = err as BusinessError;
|
|
|
+ LogUtil.error(`ImageUtil-pixelMapToBase64Str-异常 ~ code: ${error.code} -·- message: ${error.message}`);
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 图片压缩或重新打包,使用Promise形式返回结果。
|
|
|
+ * @param source PixelMap-打包的PixelMap资源。
|
|
|
+ * @param options 设置打包参数:
|
|
|
+ * format 目标格式。当前只支持"image/jpeg"、"image/webp"、"image/png"和"image/heif"12+(不同硬件设备支持情况不同)。
|
|
|
+ * quality JPEG编码中设定输出图片质量的参数,取值范围为0-100。
|
|
|
+ * bufferSize 接收编码数据的缓冲区大小,单位为Byte。默认为10MB。bufferSize需大于编码后图片大小。
|
|
|
+ * @returns
|
|
|
+ */
|
|
|
+ static packingFromPixelMap(source: image.PixelMap, options: image.PackingOption): Promise<ArrayBuffer> {
|
|
|
+ const imagePacker: image.ImagePacker = image.createImagePacker();
|
|
|
+ return imagePacker.packToData(source, options).finally(() => {
|
|
|
+ imagePacker.release(); //释放
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
export default new ImageUtils();
|