From 2a01e5ea4dee9565496dbfbc74e96976023f09b3 Mon Sep 17 00:00:00 2001 From: sijainguo Date: Mon, 29 Apr 2024 11:23:35 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=8D=E9=87=87=E6=A0=B7=E9=80=82=E9=85=8DSV?= =?UTF-8?q?G=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../imageknife/Downsampling/Downsampler.ets | 210 +++++++++--------- .../imageknife/utils/ParseImageUtil.ets | 7 +- .../imageknife/utils/svg/SVGParseImpl.ets | 23 ++ 3 files changed, 139 insertions(+), 101 deletions(-) diff --git a/library/src/main/ets/components/imageknife/Downsampling/Downsampler.ets b/library/src/main/ets/components/imageknife/Downsampling/Downsampler.ets index da8ac1d..b2cfa5f 100644 --- a/library/src/main/ets/components/imageknife/Downsampling/Downsampler.ets +++ b/library/src/main/ets/components/imageknife/Downsampling/Downsampler.ets @@ -5,13 +5,14 @@ import { FileTypeUtil } from '../utils/FileTypeUtil'; import { CenterOutside, FitCenter, SampleSizeRounding } from './DownsampleStartegy'; let TAG = 'downsampling' + export class Downsampler { calculateScaling( imageType: ArrayBuffer, - sourceHeight:number, - sourceWidth:number, + sourceHeight: number, + sourceWidth: number, request?: RequestOption - ):ESObject { + ): ESObject { const fileType: string | null = new FileTypeUtil().getFileType(imageType) let powerOfTwoWidth: number | null = null; let powerOfTwoHeight: number | null = null; @@ -39,103 +40,112 @@ export class Downsampler { if (exactScaleFactor <= 0) { throw new Error("Cannot round with exactScaleFactor"); } - console.log('exactScaleFactor', exactScaleFactor) - /*安卓的模式*/ - let rounding: SampleSizeRounding = CenterOutside.getSampleSizeRounding(orientedSourceWidth, orientedSourceHeight, targetWidth, targetHeight) - if (rounding == null) { - throw new Error("Cannot round with null rounding"); - } - let outWidth: number = this.round(exactScaleFactor * orientedSourceWidth); - let outHeight: number = this.round(exactScaleFactor * orientedSourceHeight); - let widthScaleFactor = Math.floor(orientedSourceWidth / outWidth); - let heightScaleFactor = Math.floor(orientedSourceHeight / outHeight); - let scaleFactor = rounding == SampleSizeRounding.MEMORY ? Math.max(widthScaleFactor, heightScaleFactor) : Math.min(widthScaleFactor, heightScaleFactor) - let powerOfTwoSampleSize: number; - powerOfTwoSampleSize = Math.max(1, this.highestOneBit(scaleFactor)); - if (fileType == "JPEG") { - let nativeScaling = Math.min(powerOfTwoSampleSize, 8); - powerOfTwoWidth = Math.ceil(orientedSourceWidth / nativeScaling); - powerOfTwoHeight = Math.ceil(orientedSourceHeight / nativeScaling); - let secondaryScaling = Math.floor(powerOfTwoSampleSize / 8); - if (secondaryScaling > 0) { - powerOfTwoWidth = powerOfTwoWidth / secondaryScaling; - powerOfTwoHeight = powerOfTwoHeight / secondaryScaling; - } - } else if (fileType == "PNG") { - powerOfTwoWidth = Math.floor(orientedSourceWidth / powerOfTwoSampleSize); - powerOfTwoHeight = Math.floor(orientedSourceHeight / powerOfTwoSampleSize); - console.log('执行了没', powerOfTwoHeight, powerOfTwoWidth) - } else if (fileType == "WEBP") { - powerOfTwoWidth = Math.round(orientedSourceWidth / powerOfTwoSampleSize); - powerOfTwoHeight = Math.round(orientedSourceHeight / powerOfTwoSampleSize); - } else if ( - orientedSourceWidth % powerOfTwoSampleSize != 0 || orientedSourceHeight % powerOfTwoSampleSize != 0) { - - - // let dimensions; number[] = this.getDimensions(imageReader, options, decodeCallbacks,bitmapPool); - // powerOfTwoWidth = dimensions[0]; - // powerofTwoHeight = dimensions[1]; - } else { - powerOfTwoWidth = orientedSourceWidth / powerOfTwoSampleSize; - powerOfTwoHeight = orientedSourceHeight / powerOfTwoSampleSize; - } - // Let adjustedScaleFactor = downsampleStrategy.getScaleFactor(powerOfTwoWidth, powerOfTwoHeight, targetWidth, targetHeight); - // Density scaling is only supported if inBitmap is null prior to KitKat. Avoid setting - // densities here so we calculate the final Bitmap size correctly. - // if (Build.VERSION,SDK_INT >=Build.VERSION_CODES.KITKAT) { - // options.inTargetDensity = this.adjustTargetDensityForError(adjustedScaleFactor); - // options,inDensity = this.getDensityMultiplier(adjustedScaleFactor); - //} - // if (this.isScaling(options)){ - // options.inScaled = true; - // }else { - // options.inDensity = options.inTargetDensity =0; - // } - //} - let a: ESObject = { "targetWidth": powerOfTwoWidth, "targetHeight": powerOfTwoHeight } - return a + console.log('exactScaleFactor', exactScaleFactor) + /*安卓的模式*/ + let rounding: SampleSizeRounding = CenterOutside.getSampleSizeRounding(orientedSourceWidth, orientedSourceHeight, targetWidth, targetHeight) + if (rounding == null) { + throw new Error("Cannot round with null rounding"); } - //decodeStream(imageReader:ImageReader, options: ESObject, callbacks: DecodeCallbacks, bitmapPool: ESObject) { - //if (!options.inJustDecodeBounds){ - // callbacks.onObtainBounds(); - // imageReader.stopGrowingBuffers(); - // } - // } - // getDimensions(imageReader: ImageReader, options: ESObject, decodeCallbacks: DecodeCallbacks, bitmapPool: ESObject):number[] { - // options.inJustDecodeBounds = true; - // this.decodeStream(imageReader, options, decodeCallbacks, bitmapPool); - // options.inJustDecodeBounds =false; - // return new Array(options.outWidth, options,outHeight); - // + let outWidth: number = this.round(exactScaleFactor * orientedSourceWidth); + let outHeight: number = this.round(exactScaleFactor * orientedSourceHeight); + let widthScaleFactor = Math.floor(orientedSourceWidth / outWidth); + let heightScaleFactor = Math.floor(orientedSourceHeight / outHeight); + let scaleFactor = rounding == SampleSizeRounding.MEMORY ? Math.max(widthScaleFactor, heightScaleFactor) : Math.min(widthScaleFactor, heightScaleFactor) + // 将整型的缩放因子转换为2的次幂采样大小 + let powerOfTwoSampleSize: number; + if (rounding == SampleSizeRounding.MEMORY && powerOfTwoSampleSize < (1 / exactScaleFactor)) { + powerOfTwoSampleSize = powerOfTwoSampleSize << 1; + } + // 基于上一步得出的采样大小,根据不同的图片类型,计算采样后的图片尺寸 + if (fileType == "JPEG") { + let nativeScaling = Math.min(powerOfTwoSampleSize, 8); + powerOfTwoWidth = Math.ceil(orientedSourceWidth / nativeScaling); + powerOfTwoHeight = Math.ceil(orientedSourceHeight / nativeScaling); + let secondaryScaling = Math.floor(powerOfTwoSampleSize / 8); + if (secondaryScaling > 0) { + powerOfTwoWidth = powerOfTwoWidth / secondaryScaling; + powerOfTwoHeight = powerOfTwoHeight / secondaryScaling; + } + } else if (fileType == "PNG") { + powerOfTwoWidth = Math.floor(orientedSourceWidth / powerOfTwoSampleSize); + powerOfTwoHeight = Math.floor(orientedSourceHeight / powerOfTwoSampleSize); + console.log('执行了没', powerOfTwoHeight, powerOfTwoWidth) + } else if (fileType == "WEBP") { + powerOfTwoWidth = Math.round(orientedSourceWidth / powerOfTwoSampleSize); + powerOfTwoHeight = Math.round(orientedSourceHeight / powerOfTwoSampleSize); + } else if ( + orientedSourceWidth % powerOfTwoSampleSize != 0 || orientedSourceHeight % powerOfTwoSampleSize != 0) { + + + // let dimensions; number[] = this.getDimensions(imageReader, options, decodeCallbacks,bitmapPool); + // powerOfTwoWidth = dimensions[0]; + // powerofTwoHeight = dimensions[1]; + } else { + powerOfTwoWidth = orientedSourceWidth / powerOfTwoSampleSize; + powerOfTwoHeight = orientedSourceHeight / powerOfTwoSampleSize; + } + // Let adjustedScaleFactor = downsampleStrategy.getScaleFactor(powerOfTwoWidth, powerOfTwoHeight, targetWidth, targetHeight); + // Density scaling is only supported if inBitmap is null prior to KitKat. Avoid setting + // densities here so we calculate the final Bitmap size correctly. + // if (Build.VERSION,SDK_INT >=Build.VERSION_CODES.KITKAT) { + // options.inTargetDensity = this.adjustTargetDensityForError(adjustedScaleFactor); + // options,inDensity = this.getDensityMultiplier(adjustedScaleFactor); + //} + // if (this.isScaling(options)){ + // options.inScaled = true; + // }else { + // options.inDensity = options.inTargetDensity =0; // } + //} + let a: ESObject = { "targetWidth": powerOfTwoWidth, "targetHeight": powerOfTwoHeight } + return a + } - highestOneBit(i: number): number{ - i |= (i >> 1); - i |= (i >> 2); - i |= (i >> 4); - i |= (i >> 8); - i |= (i >> 16); - return i - (i >>> 1); - } - round(value: number): number { - return Math.floor(value +0.5); - } - isRotationRequired(degreesToRotate: number): boolean { - return degreesToRotate == 90 || degreesToRotate == 270; - } - // isScaling(options: ESObject): boolean { - // return options.inTargetDensity >0 - // && options.inDensity>0 - // && options.inTargetDensity != options.inDensity; - //} - getDensityMultiplier(adjustedScaleFactor: number):number{ - return Math.round(Number.MAX_VALUE * (adjustedScaleFactor <= 1 ? adjustedScaleFactor : 1 / adjustedScaleFactor)); - } - adjustTargetDensityForError(adjustedScaleFactor:number):number{ - let densityMultiplier = this.getDensityMultiplier(adjustedScaleFactor); - let targetDensity = this.round(densityMultiplier * adjustedScaleFactor); - let scaleFactorWithError = targetDensity / densityMultiplier; - let difference = adjustedScaleFactor / scaleFactorWithError; - return this.round(difference * targetDensity); - } - } \ No newline at end of file + //decodeStream(imageReader:ImageReader, options: ESObject, callbacks: DecodeCallbacks, bitmapPool: ESObject) { + //if (!options.inJustDecodeBounds){ + // callbacks.onObtainBounds(); + // imageReader.stopGrowingBuffers(); + // } + // } + // getDimensions(imageReader: ImageReader, options: ESObject, decodeCallbacks: DecodeCallbacks, bitmapPool: ESObject):number[] { + // options.inJustDecodeBounds = true; + // this.decodeStream(imageReader, options, decodeCallbacks, bitmapPool); + // options.inJustDecodeBounds =false; + // return new Array(options.outWidth, options,outHeight); + // + // } + + highestOneBit(i: number): number { + i |= (i >> 1); + i |= (i >> 2); + i |= (i >> 4); + i |= (i >> 8); + i |= (i >> 16); + return i - (i >>> 1); + } + + round(value: number): number { + return Math.floor(value + 0.5); + } + + isRotationRequired(degreesToRotate: number): boolean { + return degreesToRotate == 90 || degreesToRotate == 270; + } + + // isScaling(options: ESObject): boolean { + // return options.inTargetDensity >0 + // && options.inDensity>0 + // && options.inTargetDensity != options.inDensity; + //} + getDensityMultiplier(adjustedScaleFactor: number): number { + return Math.round(Number.MAX_VALUE * (adjustedScaleFactor <= 1 ? adjustedScaleFactor : 1 / adjustedScaleFactor)); + } + + adjustTargetDensityForError(adjustedScaleFactor: number): number { + let densityMultiplier = this.getDensityMultiplier(adjustedScaleFactor); + let targetDensity = this.round(densityMultiplier * adjustedScaleFactor); + let scaleFactorWithError = targetDensity / densityMultiplier; + let difference = adjustedScaleFactor / scaleFactorWithError; + return this.round(difference * targetDensity); + } +} \ No newline at end of file diff --git a/library/src/main/ets/components/imageknife/utils/ParseImageUtil.ets b/library/src/main/ets/components/imageknife/utils/ParseImageUtil.ets index 0939f90..b004153 100644 --- a/library/src/main/ets/components/imageknife/utils/ParseImageUtil.ets +++ b/library/src/main/ets/components/imageknife/utils/ParseImageUtil.ets @@ -20,7 +20,12 @@ import { RequestOption } from '../RequestOption'; import { Downsampler } from '../Downsampling/Downsampler'; export class ParseImageUtil implements IParseImage { - parseImage(imageinfo: ArrayBuffer, onCompleteFunction: (value: PixelMap) => void | PromiseLike, onErrorFunction: (reason?: BusinessError | string) => void,request?:RequestOption) { + parseImage( + imageinfo: ArrayBuffer, + onCompleteFunction: (value: PixelMap) => void | PromiseLike, + onErrorFunction: (reason?: BusinessError | string) => void, + request?:RequestOption + ) { this.parseImageThumbnail(1, imageinfo, onCompleteFunction, onErrorFunction,request) } diff --git a/library/src/main/ets/components/imageknife/utils/svg/SVGParseImpl.ets b/library/src/main/ets/components/imageknife/utils/svg/SVGParseImpl.ets index 557214a..51f0843 100644 --- a/library/src/main/ets/components/imageknife/utils/svg/SVGParseImpl.ets +++ b/library/src/main/ets/components/imageknife/utils/svg/SVGParseImpl.ets @@ -34,6 +34,29 @@ export class SVGParseImpl implements IParseSvg { editable: true, desiredSize: defaultSize }; + imageSource.getInageInfo().then((value) => { + let hValue = Math.round(value.size.height); + let wValue = Math.round(value.size.width); + let defaultSize: image.size = { + height: hValue, + width: wValue + }; + let opts: image.DecodingOptions = { + editable: true, + desiredSize: defaultSize + }; + if (option?.downsampType.getName() !== 'DownsampleNone') { + const b: ESObject = new Downsampler().calculateScaling(imageInfo, hValue, wValue, option) + console.log("bbb-----", JSON.stringify(b)) + opts = { + editable: true, + desiredSize: { + width: b.targetWidth, + height: b.targetHeight + } + } + } + }) imageSource.createPixelMap(opts).then((pixelMap: image.PixelMap) => { let imageKnifeData = ImageKnifeData.createImagePixelMap(ImageKnifeType.PIXELMAP, pixelMap) onComplete(imageKnifeData?.drawPixelMap?.imagePixelMap as PixelMap);