|
@@ -3,6 +3,9 @@ import fs, { ListFileOptions, ReadOptions, ReadTextOptions, WriteOptions } from
|
|
import { BusinessError } from '@kit.BasicServicesKit';
|
|
import { BusinessError } from '@kit.BasicServicesKit';
|
|
import { StrHelper } from './StrHelper';
|
|
import { StrHelper } from './StrHelper';
|
|
import { hilog } from '@kit.PerformanceAnalysisKit';
|
|
import { hilog } from '@kit.PerformanceAnalysisKit';
|
|
|
|
+import { image } from '@kit.ImageKit';
|
|
|
|
+import { buffer } from '@kit.ArkTS';
|
|
|
|
+import { LogHelper } from './LogHelper';
|
|
|
|
|
|
export class FileHelper {
|
|
export class FileHelper {
|
|
private constructor() {
|
|
private constructor() {
|
|
@@ -308,6 +311,30 @@ export class FileHelper {
|
|
return fs.accessSync(path);
|
|
return fs.accessSync(path);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ static BmpToBase64(photoUri: string): Promise<string> {
|
|
|
|
+ const imagePackerApi: image.ImagePacker = image.createImagePacker();
|
|
|
|
+ const packOpts: image.PackingOption = { format: 'image/jpeg', quality: 100 };
|
|
|
|
+
|
|
|
|
+ return new Promise<string>((resolve, reject) => {
|
|
|
|
+ let file = FileHelper.openSync(photoUri, fs.OpenMode.READ_ONLY)
|
|
|
|
+ const imageSource = image.createImageSource(file.fd);
|
|
|
|
+ LogHelper.error('file.fd:' + file.fd)
|
|
|
|
+
|
|
|
|
+ imageSource.createPixelMap().then((pixelMap) => {
|
|
|
|
+ imagePackerApi.packing(pixelMap, packOpts).then((data: ArrayBuffer) => {
|
|
|
|
+ let buf: buffer.Buffer = buffer.from(data);
|
|
|
|
+ LogHelper.error('data:' + data)
|
|
|
|
+ resolve(buf.toString('base64'))
|
|
|
|
+ }).catch((err: BusinessError) => {
|
|
|
|
+ LogHelper.error('packing failed with err: ' + JSON.stringify(err));
|
|
|
|
+ reject(err)
|
|
|
|
+ })
|
|
|
|
+ }).catch((err: BusinessError) => {
|
|
|
|
+ LogHelper.error('createPixelMap failed with err: ' + JSON.stringify(err));
|
|
|
|
+ reject(err)
|
|
|
|
+ });
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
* 打开文件,支持使用URI打开文件。使用Promise异步回调。
|
|
* 打开文件,支持使用URI打开文件。使用Promise异步回调。
|