@@ -6,6 +6,7 @@ import type Want from '@ohos.app.ability.Want';
6
6
import image from '@ohos.multimedia.image' ;
7
7
import media from '@ohos.multimedia.media' ;
8
8
import util from '@ohos.util' ;
9
+ import uri from '@ohos.uri' ;
9
10
import picker from '@ohos.multimedia.cameraPicker' ;
10
11
import camera from '@ohos.multimedia.camera' ;
11
12
import { BusinessError } from '@ohos.base' ;
@@ -221,6 +222,12 @@ export class ImageCropPickerTurboModule extends TurboModule implements TM.ImageC
221
222
222
223
async openPicker ( options ?: Options ) : Promise < Video [ ] | Video | ImageOrVideo [ ] | ImageOrVideo | Image [ ] | Image > {
223
224
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
+ }
224
231
let minFiles = this . isNullOrUndefined ( options ?. minFiles ) ? MinNumber : options . minFiles ;
225
232
let maxFiles = this . isNullOrUndefined ( options ?. maxFiles ) ? MaxNumber : options . maxFiles ;
226
233
let isShowSerialNum = this . isNullOrUndefined ( options ?. showsSelectedCount ) ? true : options ?. showsSelectedCount ;
@@ -395,13 +402,11 @@ export class ImageCropPickerTurboModule extends TurboModule implements TM.ImageC
395
402
async openCamera ( options ?: Options ) : Promise < Video [ ] | Video | ImageOrVideo [ ] | ImageOrVideo | Image [ ] | Image > {
396
403
Logger . info ( TAG , 'into openCamera request: ' + JSON . stringify ( options ) ) ;
397
404
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
+ } )
405
410
}
406
411
let quality = options . compressImageQuality ;
407
412
if ( ! this . isNullOrUndefined ( quality ) && ! ( quality >= 0 && quality <= 1 ) ) {
@@ -428,10 +433,19 @@ export class ImageCropPickerTurboModule extends TurboModule implements TM.ImageC
428
433
let pickerResult : picker . PickerResult = await picker . pick ( mContext , mediaType , pickerProfile ) ;
429
434
Logger . info ( TAG , 'into openCamera results: ' + JSON . stringify ( pickerResult ) ) ;
430
435
let imgOrVideoPath = pickerResult . resultUri ;
431
- let imgCropPath = cropping ? await this . intoCropper ( imgOrVideoPath , options ) : '' ;
432
- Logger . info ( TAG , 'into openCamera imgCropPath = ' + imgCropPath ) ;
433
436
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
+ }
435
449
let tempFilePaths = null ;
436
450
let sourceFilePaths : Array < string > = [ imgOrVideoPath ] ;
437
451
if ( qualityNumber !== 1 || forceJpg ) {
@@ -453,9 +467,14 @@ export class ImageCropPickerTurboModule extends TurboModule implements TM.ImageC
453
467
Logger . error ( TAG , 'into openCamera err :' + JSON . stringify ( err ) ) ;
454
468
}
455
469
}
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
+ }
456
475
this . getFileInfo ( includeBase64 , imgOrVideoPath , tempFilePath , exifInfo ) . then ( ( imageInfo ) => {
457
476
imgResult . sourceURL = imgOrVideoPath ;
458
- imgResult . path = this . isNullOrUndefined ( tempFilePath ) ? imgOrVideoPath : filePrefix + tempFilePath ;
477
+ imgResult . path = this . isNullOrUndefined ( tempFilePath ) ? filePrefix + imgOrVideoPath : filePrefix + tempFilePath ;
459
478
imgResult . exif = imageInfo . exif ;
460
479
imgResult . data = imageInfo . data ;
461
480
imgResult . size = imageInfo . size ;
@@ -495,10 +514,28 @@ export class ImageCropPickerTurboModule extends TurboModule implements TM.ImageC
495
514
496
515
imageToBase64 ( filePath : string ) : string {
497
516
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
+ }
498
535
let base64Data ;
499
536
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 ) ;
502
539
Logger . info ( TAG , 'into imageToBase64 stat.size = ' + stat . size ) ;
503
540
let buf = new ArrayBuffer ( stat . size ) ;
504
541
fs . readSync ( file . fd , buf ) ;
@@ -616,6 +653,12 @@ export class ImageCropPickerTurboModule extends TurboModule implements TM.ImageC
616
653
617
654
async openCropper ( options ?: CropperOptions ) : Promise < Image > {
618
655
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
+ }
619
662
let permissionResult = await this . grantPermission ( ) ;
620
663
if ( ! permissionResult ) {
621
664
return new Promise ( async ( res , rej ) => {
@@ -633,7 +676,6 @@ export class ImageCropPickerTurboModule extends TurboModule implements TM.ImageC
633
676
let writeTempFile = this . isNullOrUndefined ( options ?. writeTempFile ) ? true :options ?. writeTempFile ;
634
677
let qualityNumber = this . isNullOrUndefined ( options . compressImageQuality ) ? ImageQuality : options . compressImageQuality ;
635
678
let forceJpg = this . isNullOrUndefined ( options . forceJpg ) ? false : options . forceJpg ;
636
- let path = options ?. path ;
637
679
let imgPath = await this . intoCropper ( path , options ) ;
638
680
Logger . info ( TAG , 'into openCropper imgPath = ' + imgPath ) ;
639
681
if ( this . isNullOrUndefined ( imgPath ) ) {
@@ -662,7 +704,7 @@ export class ImageCropPickerTurboModule extends TurboModule implements TM.ImageC
662
704
}
663
705
return new Promise ( async ( res , rej ) => {
664
706
this . getFileInfo ( options ?. includeBase64 , imgPath , tempFilePath , exifInfo ) . then ( ( imageInfo ) => {
665
- result . path = filePrefix + imgPath ;
707
+ result . path = this . isNullOrUndefined ( tempFilePath ) ? filePrefix + imgPath : filePrefix + tempFilePath ;
666
708
result . exif = imageInfo . exif ;
667
709
result . sourceURL = options ?. path ;
668
710
result . data = imageInfo . data ;
0 commit comments