@tarojs/plugin-platform-harmony-ets 4.0.0-beta.112 → 4.0.0-beta.113

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/runtime.js CHANGED
@@ -2280,6 +2280,10 @@ const chooseMedia = function (options) {
2280
2280
  };
2281
2281
 
2282
2282
  const READ_IMAGEVIDEO_PERMISSIONS = 'ohos.permission.READ_IMAGEVIDEO';
2283
+ const READ_MEDIA_PERMISSIONS = 'ohos.permission.READ_MEDIA';
2284
+ const WRITE_MEDIA_PERMISSIONS = 'ohos.permission.WRITE_MEDIA';
2285
+ const MEDIA_LOCATION_PERMISSIONS = 'ohos.permission.MEDIA_LOCATION';
2286
+ const IMAGE_PERMISSION = [READ_IMAGEVIDEO_PERMISSIONS, READ_MEDIA_PERMISSIONS, WRITE_MEDIA_PERMISSIONS, MEDIA_LOCATION_PERMISSIONS];
2283
2287
 
2284
2288
  // HarmonyOS 图片模块首批接口从API version 7开始支持。
2285
2289
  // HarmonyOS 文档链接:https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-apis-image-0000001122977382
@@ -2326,12 +2330,17 @@ class CompressedImageInfo {
2326
2330
  }
2327
2331
  function saveImage(compressedImageData, compressedImageUri) {
2328
2332
  return __awaiter(this, void 0, void 0, function* () {
2329
- // 定义要保存的压缩图片uri。afterCompressiona.jpeg表示压缩后的图片。
2333
+ const tempArr = compressedImageUri.split('/');
2334
+ const name = tempArr[tempArr.length - 1];
2335
+ const context = getContext(Current === null || Current === void 0 ? void 0 : Current.page);
2336
+ const applicationContext = context.getApplicationContext();
2337
+ const tempDir = applicationContext.tempDir;
2338
+ const filePath = `${tempDir}/${name}`;
2330
2339
  try {
2331
- const res = fs.accessSync(compressedImageUri);
2340
+ const res = fs.accessSync(filePath);
2332
2341
  if (res) {
2333
2342
  // 如果图片afterCompressiona.jpeg已存在,则删除
2334
- fs.unlinkSync(compressedImageUri);
2343
+ fs.unlinkSync(filePath);
2335
2344
  }
2336
2345
  }
2337
2346
  catch (err) {
@@ -2339,19 +2348,19 @@ function saveImage(compressedImageData, compressedImageUri) {
2339
2348
  }
2340
2349
  // 知识点:保存图片。获取最终图片压缩数据compressedImageData,保存图片。
2341
2350
  // 压缩图片数据写入文件
2342
- const file = fs.openSync(compressedImageUri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
2351
+ const file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
2343
2352
  fs.writeSync(file.fd, compressedImageData);
2344
2353
  fs.closeSync(file);
2345
2354
  // 获取压缩图片信息
2346
2355
  const compressedImageInfo = new CompressedImageInfo();
2347
- compressedImageInfo.imageUri = compressedImageUri;
2356
+ compressedImageInfo.imageUri = filePath;
2348
2357
  compressedImageInfo.imageByteLength = compressedImageData.byteLength;
2349
2358
  return compressedImageInfo;
2350
2359
  });
2351
2360
  }
2352
2361
  const compressImage = function (options) {
2353
2362
  return new Promise((resolve, reject) => {
2354
- requestPermissions([READ_IMAGEVIDEO_PERMISSIONS]).then(() => {
2363
+ requestPermissions(IMAGE_PERMISSION).then(() => {
2355
2364
  try {
2356
2365
  validateParams('compressImage', options, compressImageSchema);
2357
2366
  }
@@ -2359,8 +2368,8 @@ const compressImage = function (options) {
2359
2368
  const res = { errMsg: error.message };
2360
2369
  return callAsyncFail(reject, res, options);
2361
2370
  }
2362
- const { src, quality = 80 } = options;
2363
- const srcAfterCompress = src.split('.').join('_after_compress.');
2371
+ const { src, quality = 80, compressedWidth, compressedHeight } = options;
2372
+ const srcAfterCompress = src.includes('_after_compress') ? src : src.split('.').join('_after_compress.');
2364
2373
  const file = fs.openSync(src, fs.OpenMode.READ_ONLY);
2365
2374
  // const stat = fs.statSync(file.fd)
2366
2375
  // console.log('[Taro] 压缩前图片的大小为:', stat.size)
@@ -2370,24 +2379,52 @@ const compressImage = function (options) {
2370
2379
  callAsyncFail(reject, createImageSourceError, options);
2371
2380
  return;
2372
2381
  }
2373
- const packer = image.createImagePacker(file.fd);
2374
- if (isNull(packer)) {
2375
- const createImagePackerError = { errMsg: 'compressImage fail: createImagePacker has failed.' };
2376
- callAsyncFail(reject, createImagePackerError, options);
2382
+ const width = source.getImageInfoSync().size.width;
2383
+ const height = source.getImageInfoSync().size.height;
2384
+ let wantWidth = compressedWidth || compressedHeight || 0;
2385
+ let wantHeight = compressedHeight || compressedWidth || 0;
2386
+ if (width > wantWidth || height > wantHeight) {
2387
+ const heightRatio = height / wantHeight;
2388
+ const widthRatio = width / wantWidth;
2389
+ const finalRatio = heightRatio < widthRatio ? heightRatio : widthRatio;
2390
+ wantWidth = Math.round(width / finalRatio);
2391
+ wantHeight = Math.round(height / finalRatio);
2377
2392
  }
2378
- const packingOptionsOHOS = {
2379
- // TODO:需要获取文件名后缀
2380
- format: 'image/jpeg',
2381
- quality: quality
2393
+ const decodingOptions = {
2394
+ editable: true,
2395
+ desiredPixelFormat: image.PixelMapFormat.RGBA_8888,
2396
+ desiredSize: { width: wantWidth, height: wantHeight }
2382
2397
  };
2383
- packer.packing(source, packingOptionsOHOS).then((value) => {
2384
- saveImage(value, srcAfterCompress).then(result => {
2385
- callAsyncSuccess(resolve, { tempFilePath: result.imageUri }, options);
2386
- });
2387
- }).catch((error) => {
2388
- callAsyncFail(reject, error, options);
2398
+ source.createPixelMap(decodingOptions, (error, pixelMap) => {
2399
+ if (error !== undefined) {
2400
+ fs.closeSync(file);
2401
+ const res = { errMsg: error };
2402
+ callAsyncFail(reject, res, options);
2403
+ }
2404
+ else {
2405
+ const packer = image.createImagePacker(file.fd);
2406
+ if (isNull(packer)) {
2407
+ fs.closeSync(file);
2408
+ const createImagePackerError = { errMsg: 'compressImage fail: createImagePacker has failed.' };
2409
+ callAsyncFail(reject, createImagePackerError, options);
2410
+ return;
2411
+ }
2412
+ const isPNG = src.endsWith('.png');
2413
+ const packingOptionsOHOS = {
2414
+ format: isPNG ? 'image/png' : 'image/jpeg',
2415
+ quality: quality
2416
+ };
2417
+ packer.packing(pixelMap, packingOptionsOHOS).then((value) => {
2418
+ fs.closeSync(file);
2419
+ saveImage(value, srcAfterCompress).then(result => {
2420
+ callAsyncSuccess(resolve, { tempFilePath: result.imageUri }, options);
2421
+ });
2422
+ }).catch((error) => {
2423
+ fs.closeSync(file);
2424
+ callAsyncFail(reject, error, options);
2425
+ });
2426
+ }
2389
2427
  });
2390
- fs.closeSync(file);
2391
2428
  }, (error) => {
2392
2429
  const res = { errMsg: error };
2393
2430
  return callAsyncFail(reject, res, options);
@@ -2396,7 +2433,7 @@ const compressImage = function (options) {
2396
2433
  };
2397
2434
  const chooseImage = function (options) {
2398
2435
  return new Promise((resolve, reject) => {
2399
- requestPermissions([READ_IMAGEVIDEO_PERMISSIONS]).then(() => {
2436
+ requestPermissions(IMAGE_PERMISSION).then(() => {
2400
2437
  try {
2401
2438
  validateParams('chooseImage', options, chooseImageSchema);
2402
2439
  }
@@ -2414,23 +2451,31 @@ const chooseImage = function (options) {
2414
2451
  photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE; // 过滤选择媒体文件类型为IMAGE
2415
2452
  photoViewPicker.select(photoSelectOptions).then((photoSelectResult) => {
2416
2453
  const result = {};
2417
- if (sizeType.includes('original')) {
2454
+ const isOrigin = photoSelectResult.isOriginalPhoto;
2455
+ if (isOrigin) {
2456
+ const tempFilePaths = [];
2418
2457
  const tempFiles = photoSelectResult.photoUris.map(uri => {
2419
2458
  const file = fs.openSync(uri, fs.OpenMode.READ_ONLY);
2420
2459
  const stat = fs.statSync(file.fd);
2421
2460
  const size = stat.size;
2461
+ fs.closeSync(file);
2462
+ tempFilePaths.push(uri);
2422
2463
  return {
2423
2464
  size,
2424
2465
  path: uri,
2425
2466
  };
2426
2467
  });
2427
2468
  result.tempFiles = tempFiles;
2469
+ result.tempFilePaths = tempFilePaths;
2470
+ callAsyncSuccess(resolve, result, options);
2428
2471
  }
2429
- if (sizeType.includes('compressed')) {
2472
+ else {
2430
2473
  const actions = photoSelectResult.photoUris.map(uri => {
2431
2474
  return new Promise(resolve => {
2432
2475
  compressImage({
2433
2476
  src: uri,
2477
+ compressedWidth: getSystemInfoSync().screenWidth / 2,
2478
+ compressedHeight: getSystemInfoSync().screenHeight / 2,
2434
2479
  success: (compressResult) => {
2435
2480
  resolve(compressResult.tempFilePath);
2436
2481
  }
@@ -2438,16 +2483,24 @@ const chooseImage = function (options) {
2438
2483
  });
2439
2484
  });
2440
2485
  Promise.all(actions).then(tempFilePaths => {
2486
+ const tempFiles = tempFilePaths.map(uri => {
2487
+ const file = fs.openSync(uri, fs.OpenMode.READ_ONLY);
2488
+ const stat = fs.statSync(file.fd);
2489
+ const size = stat.size;
2490
+ fs.closeSync(file);
2491
+ return {
2492
+ size,
2493
+ path: uri,
2494
+ };
2495
+ });
2441
2496
  result.tempFilePaths = tempFilePaths;
2497
+ result.tempFiles = tempFiles;
2442
2498
  callAsyncSuccess(resolve, result, options);
2443
2499
  }).catch(error => {
2444
2500
  const res = { errMsg: error };
2445
2501
  return callAsyncFail(reject, res, options);
2446
2502
  });
2447
2503
  }
2448
- else {
2449
- callAsyncSuccess(resolve, result, options);
2450
- }
2451
2504
  }).catch((error) => {
2452
2505
  callAsyncFail(reject, error, options);
2453
2506
  });