Skip to content
This repository was archived by the owner on Dec 13, 2024. It is now read-only.

Commit 89f04ec

Browse files
authored
Merge pull request #15 from lankun1009/sig
fix: 解决oepnPicker,openCamera媒体路径图片无法获取base64转码问题
2 parents a1f256a + 7fa4e70 commit 89f04ec

File tree

4 files changed

+59
-17
lines changed

4 files changed

+59
-17
lines changed

harmony/image_crop_picker.har

91 Bytes
Binary file not shown.

harmony/image_crop_picker/oh-package.json5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Please describe the basic information.",
44
main: 'index.ets',
55
type: 'module',
6-
version: '0.0.5',
6+
version: '0.0.6',
77
dependencies: {
88
"@rnoh/react-native-openharmony": "file:../react_native_openharmony"
99
},

harmony/image_crop_picker/src/main/ets/ImageCropPickerTurboModule.ts

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type Want from '@ohos.app.ability.Want';
66
import image from '@ohos.multimedia.image';
77
import media from '@ohos.multimedia.media';
88
import util from '@ohos.util';
9+
import uri from '@ohos.uri';
910
import picker from '@ohos.multimedia.cameraPicker';
1011
import camera from '@ohos.multimedia.camera';
1112
import { BusinessError } from '@ohos.base';
@@ -221,6 +222,12 @@ export class ImageCropPickerTurboModule extends TurboModule implements TM.ImageC
221222

222223
async openPicker(options?: Options): Promise<Video[] | Video | ImageOrVideo[] | ImageOrVideo | Image [] | Image> {
223224
Logger.info(TAG, 'into openPicker request' + JSON.stringify(options));
225+
let permissionResult = await this.grantPermission();
226+
if (!permissionResult) {
227+
return new Promise(async(res, rej) => {
228+
rej('permission not approved')
229+
})
230+
}
224231
let minFiles = this.isNullOrUndefined(options?.minFiles) ? MinNumber : options.minFiles;
225232
let maxFiles = this.isNullOrUndefined(options?.maxFiles) ? MaxNumber : options.maxFiles;
226233
let isShowSerialNum = this.isNullOrUndefined(options?.showsSelectedCount) ? true : options?.showsSelectedCount;
@@ -395,13 +402,11 @@ export class ImageCropPickerTurboModule extends TurboModule implements TM.ImageC
395402
async openCamera(options?: Options): Promise<Video[] | Video | ImageOrVideo[] | ImageOrVideo | Image [] | Image> {
396403
Logger.info(TAG, 'into openCamera request: ' + JSON.stringify(options));
397404
let cropping = this.isNullOrUndefined(options?.cropping) ? false : options?.cropping;
398-
if (cropping) {
399-
let permissionResult = await this.grantPermission();
400-
if (!permissionResult) {
401-
return new Promise(async(res, rej) => {
402-
rej('permission not approved')
403-
})
404-
}
405+
let permissionResult = await this.grantPermission();
406+
if (!permissionResult) {
407+
return new Promise(async(res, rej) => {
408+
rej('permission not approved')
409+
})
405410
}
406411
let quality = options.compressImageQuality;
407412
if (!this.isNullOrUndefined(quality) && !(quality >= 0 && quality <= 1)) {
@@ -428,10 +433,19 @@ export class ImageCropPickerTurboModule extends TurboModule implements TM.ImageC
428433
let pickerResult: picker.PickerResult = await picker.pick(mContext, mediaType, pickerProfile);
429434
Logger.info(TAG, 'into openCamera results: ' + JSON.stringify(pickerResult));
430435
let imgOrVideoPath = pickerResult.resultUri;
431-
let imgCropPath = cropping ? await this.intoCropper(imgOrVideoPath, options) : '';
432-
Logger.info(TAG, 'into openCamera imgCropPath = ' + imgCropPath);
433436
isImg = this.isImage(imgOrVideoPath);
434-
437+
if (isImg) {
438+
let file = fs.openSync(imgOrVideoPath, fs.OpenMode.CREATE);
439+
try {
440+
let dstPath = this.ctx.uiAbilityContext.tempDir + '/rn_image_crop_picker_lib_temp_' + util.generateRandomUUID(true) + '.jpeg';
441+
fs.copyFileSync(file.fd, dstPath, 0);
442+
imgOrVideoPath = dstPath;
443+
Logger.info(TAG, 'into openCamera suc dstPath = '+ dstPath);
444+
} catch (err) {
445+
Logger.error(TAG, 'into openCamera fail err = '+ err.toString());
446+
}
447+
fs.closeSync(file);
448+
}
435449
let tempFilePaths = null;
436450
let sourceFilePaths : Array<string> = [imgOrVideoPath];
437451
if (qualityNumber !== 1 || forceJpg) {
@@ -453,9 +467,14 @@ export class ImageCropPickerTurboModule extends TurboModule implements TM.ImageC
453467
Logger.error(TAG, 'into openCamera err :' + JSON.stringify(err));
454468
}
455469
}
470+
let imgCropPath = cropping ? await this.intoCropper(imgOrVideoPath, options) : '';
471+
if (!this.isNullOrUndefined(imgCropPath)) {
472+
tempFilePath = imgCropPath;
473+
Logger.info(TAG, 'into openCamera imgCropPath = ' + imgCropPath);
474+
}
456475
this.getFileInfo(includeBase64, imgOrVideoPath, tempFilePath, exifInfo).then((imageInfo)=>{
457476
imgResult.sourceURL = imgOrVideoPath;
458-
imgResult.path = this.isNullOrUndefined(tempFilePath) ? imgOrVideoPath : filePrefix + tempFilePath;
477+
imgResult.path = this.isNullOrUndefined(tempFilePath) ? filePrefix + imgOrVideoPath : filePrefix + tempFilePath;
459478
imgResult.exif = imageInfo.exif;
460479
imgResult.data = imageInfo.data;
461480
imgResult.size = imageInfo.size;
@@ -495,10 +514,28 @@ export class ImageCropPickerTurboModule extends TurboModule implements TM.ImageC
495514

496515
imageToBase64(filePath: string): string {
497516
Logger.info(TAG, 'into imageToBase64 filePath: ' + filePath);
517+
let uriPath = new uri.URI(filePath);
518+
let dstPath = filePath;
519+
if (uriPath.host === 'media') {
520+
let i = filePath.lastIndexOf('.');
521+
let imageType;
522+
if (i != -1) {
523+
imageType = filePath.substring(i + 1);
524+
}
525+
let file = fs.openSync(filePath, fs.OpenMode.CREATE);
526+
try {
527+
dstPath = this.ctx.uiAbilityContext.tempDir + '/rn_image_crop_picker_lib_temp_' + util.generateRandomUUID(true) + '.' + imageType;
528+
fs.copyFileSync(file.fd, dstPath, 0);
529+
Logger.info(TAG, 'into imageToBase64 suc dstPath = '+ dstPath);
530+
} catch (err) {
531+
Logger.error(TAG, 'into imageToBase64 fail err = '+ err.toString());
532+
}
533+
fs.closeSync(file);
534+
}
498535
let base64Data;
499536
try {
500-
let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY);
501-
let stat = fs.lstatSync(filePath);
537+
let file = fs.openSync(dstPath, fs.OpenMode.READ_ONLY);
538+
let stat = fs.lstatSync(dstPath);
502539
Logger.info(TAG, 'into imageToBase64 stat.size = ' + stat.size);
503540
let buf = new ArrayBuffer(stat.size);
504541
fs.readSync(file.fd, buf);
@@ -616,6 +653,12 @@ export class ImageCropPickerTurboModule extends TurboModule implements TM.ImageC
616653

617654
async openCropper(options?: CropperOptions): Promise<Image>{
618655
Logger.info(TAG, 'into openCropper : ' + JSON.stringify(options));
656+
let path = options?.path;
657+
if (this.isNullOrUndefined(path?.trim())) {
658+
return new Promise(async(res, rej) => {
659+
rej('path is empty')
660+
})
661+
}
619662
let permissionResult = await this.grantPermission();
620663
if (!permissionResult) {
621664
return new Promise(async(res, rej) => {
@@ -633,7 +676,6 @@ export class ImageCropPickerTurboModule extends TurboModule implements TM.ImageC
633676
let writeTempFile = this.isNullOrUndefined(options?.writeTempFile) ? true :options?.writeTempFile;
634677
let qualityNumber = this.isNullOrUndefined(options.compressImageQuality) ? ImageQuality : options.compressImageQuality;
635678
let forceJpg = this.isNullOrUndefined(options.forceJpg) ? false : options.forceJpg;
636-
let path = options?.path;
637679
let imgPath = await this.intoCropper(path, options);
638680
Logger.info(TAG, 'into openCropper imgPath = ' + imgPath);
639681
if (this.isNullOrUndefined(imgPath)) {
@@ -662,7 +704,7 @@ export class ImageCropPickerTurboModule extends TurboModule implements TM.ImageC
662704
}
663705
return new Promise(async (res, rej) => {
664706
this.getFileInfo(options?.includeBase64, imgPath, tempFilePath, exifInfo).then((imageInfo) => {
665-
result.path = filePrefix + imgPath;
707+
result.path = this.isNullOrUndefined(tempFilePath) ? filePrefix + imgPath : filePrefix + tempFilePath;
666708
result.exif = imageInfo.exif;
667709
result.sourceURL = options?.path;
668710
result.data = imageInfo.data;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@react-native-oh-tpl/react-native-image-crop-picker",
3-
"version": "0.40.3-0.0.5",
3+
"version": "0.40.3-0.0.6",
44
"description": "Select single or multiple images, with cropping option",
55
"main": "js/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)