diff --git a/library/src/main/ets/components/imageknife/Downsampling/BaseDownsampling.ets b/library/src/main/ets/components/imageknife/Downsampling/BaseDownsampling.ets index fa7add3..9ade775 100644 --- a/library/src/main/ets/components/imageknife/Downsampling/BaseDownsampling.ets +++ b/library/src/main/ets/components/imageknife/Downsampling/BaseDownsampling.ets @@ -1,4 +1,7 @@ +import {lang}from '@kit.ArkTs'; +type ISendable=lang.ISendable; export interface BaseDownsampling{ + getName():string getScaleFactor(sourceWidth:number, sourceHeight:number, requestWidth:number, requestHeight:number):number diff --git a/library/src/main/ets/components/imageknife/Downsampling/DownsampleStartegy.ets b/library/src/main/ets/components/imageknife/Downsampling/DownsampleStartegy.ets index 01fe132..3d6583d 100644 --- a/library/src/main/ets/components/imageknife/Downsampling/DownsampleStartegy.ets +++ b/library/src/main/ets/components/imageknife/Downsampling/DownsampleStartegy.ets @@ -1,7 +1,8 @@ import { BaseDownsampling } from './BaseDownsampling' - +@Sendable export class CenterInside{ + getName() { return "CenterInside"} getScaleFactor(sourceWidth:number, sourceHeight:number,requestWidth:number, requestHeight:number):number { return Math.min(1,FitCenter.getScaleFactor(sourceWidth, sourceHeight, requestWidth, requestHeight)) @@ -15,7 +16,11 @@ export class CenterInside{ } /*不进行下采样*/ +@Sendable export class DownsampleNone implements BaseDownsampling{ + getName(){ + return "DownsampleNone" + } getScaleFactor(sourceWidth: number, sourceHeight: number, requestWidth: number, requestHeight: number): number { return 1 } @@ -26,7 +31,11 @@ export class DownsampleNone implements BaseDownsampling{ } /* 下采样使得图像的组大尺寸在给定的尺寸的1/2之间*/ +@Sendable export class AtMost implements BaseDownsampling{ + getName(){ + return "AtMost" + } getScaleFactor(sourceWidth: number, sourceHeight: number, requestWidth:number,requestHeight: number): number { let maxIntegerFactor=Math.ceil(Math.max(sourceHeight/requestHeight,sourceWidth/requestWidth)); let lesserOrEqualSampleSize = Math.max(1,highestOneBit(maxIntegerFactor)) @@ -37,7 +46,12 @@ export class DownsampleNone implements BaseDownsampling{ return SampleSizeRounding.MEMORY } } + +@Sendable export class Atleast implements BaseDownsampling{ + getName(){ + return "Atleast" + } getScaleFactor(sourceWidth: number, sourceHeight: number, requestWidth: number, requestHeight: number): number { let minIntegerFactor=Math.floor(Math.min(sourceHeight/requestHeight,sourceWidth/requestWidth)) @@ -47,7 +61,12 @@ export class Atleast implements BaseDownsampling{ return SampleSizeRounding.QUALITY } } + +@Sendable export class CenterOutside implements BaseDownsampling{ + getName(){ + return "CenterOutside" + } getScaleFactor(sourceWidth: number, sourceHeight: number, requestWidth: number, requestHeight: number): number { let widthPercentage =requestWidth/sourceWidth let heightPercentage =requestHeight/sourceHeight @@ -60,8 +79,11 @@ export class CenterOutside implements BaseDownsampling{ } - +@Sendable export class FitCenter{ + getName(){ + return "FitCenter" + } public static getScaleFactor(sourceWidth: number, sourceHeight: number, requestWidth: number, requestHeight: number): number { let widthPercentage =requestWidth/sourceWidth let heightPercentage =requestHeight/sourceHeight diff --git a/library/src/main/ets/components/imageknife/Downsampling/Downsampler.ets b/library/src/main/ets/components/imageknife/Downsampling/Downsampler.ets index 2bf9a9e..da8ac1d 100644 --- a/library/src/main/ets/components/imageknife/Downsampling/Downsampler.ets +++ b/library/src/main/ets/components/imageknife/Downsampling/Downsampler.ets @@ -1,8 +1,9 @@ /* * asdfasdfasdfasdfasdf*/ -import { CenterOutside, FitCenter, SampleSizeRounding } from './Downsamplestrategy'; -import { FileTypeUtil } from '../../utits/Fitetypeutit'; -import { RequestOption } from '../../Requestoption'; +import { RequestOption } from '../RequestOption'; +import { FileTypeUtil } from '../utils/FileTypeUtil'; +import { CenterOutside, FitCenter, SampleSizeRounding } from './DownsampleStartegy'; + let TAG = 'downsampling' export class Downsampler { calculateScaling( @@ -26,7 +27,7 @@ export class Downsampler { } else { console.log('宽高都不存在') } - if (sourceWidth <= 0 || sourceleight <= 0) return; + if (sourceWidth <= 0 || sourceHeight <= 0) return; let orientedSourceWidth = sourceWidth; let orientedSourceHeight = sourceHeight; if (this.isRotationRequired(90)) { @@ -52,7 +53,7 @@ export class Downsampler { let powerOfTwoSampleSize: number; powerOfTwoSampleSize = Math.max(1, this.highestOneBit(scaleFactor)); if (fileType == "JPEG") { - let nativeScaling = Hath.min(powerOfTwoSampleSize, 8); + let nativeScaling = Math.min(powerOfTwoSampleSize, 8); powerOfTwoWidth = Math.ceil(orientedSourceWidth / nativeScaling); powerOfTwoHeight = Math.ceil(orientedSourceHeight / nativeScaling); let secondaryScaling = Math.floor(powerOfTwoSampleSize / 8); @@ -62,11 +63,11 @@ export class Downsampler { } } else if (fileType == "PNG") { powerOfTwoWidth = Math.floor(orientedSourceWidth / powerOfTwoSampleSize); - PowerOfTwoHeight = Math.floor(orientedSourceHeight / powerOfTwoSamplesize); + powerOfTwoHeight = Math.floor(orientedSourceHeight / powerOfTwoSampleSize); console.log('执行了没', powerOfTwoHeight, powerOfTwoWidth) } else if (fileType == "WEBP") { powerOfTwoWidth = Math.round(orientedSourceWidth / powerOfTwoSampleSize); - poWerOfTwoHeight = Math.round(orientedSourceHeight / powerOfTwoSampleSize); + powerOfTwoHeight = Math.round(orientedSourceHeight / powerOfTwoSampleSize); } else if ( orientedSourceWidth % powerOfTwoSampleSize != 0 || orientedSourceHeight % powerOfTwoSampleSize != 0) { @@ -91,7 +92,7 @@ export class Downsampler { // options.inDensity = options.inTargetDensity =0; // } //} - let a: ESObject = { "targetWidth": power0fTwoWidth, "targetHeight": powerOfTuoHeight } + let a: ESObject = { "targetWidth": powerOfTwoWidth, "targetHeight": powerOfTwoHeight } return a } //decodeStream(imageReader:ImageReader, options: ESObject, callbacks: DecodeCallbacks, bitmapPool: ESObject) { @@ -108,7 +109,7 @@ export class Downsampler { // // } - highest0neBit(i: number): number{ + highestOneBit(i: number): number{ i |= (i >> 1); i |= (i >> 2); i |= (i >> 4); diff --git a/library/src/main/ets/components/imageknife/ImageKnife.ets b/library/src/main/ets/components/imageknife/ImageKnife.ets index 3e784a1..8108f8b 100644 --- a/library/src/main/ets/components/imageknife/ImageKnife.ets +++ b/library/src/main/ets/components/imageknife/ImageKnife.ets @@ -563,6 +563,7 @@ export class ImageKnife { data.setDiskMemoryCachePath(this.diskMemoryCache.getPath()) data.setPlaceHolderRegisterCacheKey(request.placeholderRegisterCacheKey); data.setPlaceHolderRegisterMemoryCacheKey(request.placeholderRegisterMemoryCacheKey); + data.setDownsampType(request.downsampType) return data; } @@ -811,7 +812,7 @@ async function taskExecute(sendData:SendableData,taskData:TaskParams): Promise

{ parseImage(imageinfo: ArrayBuffer, onCompleteFunction: (value: PixelMap) => void | PromiseLike, onErrorFunction: (reason?: BusinessError | string) => void,request?:RequestOption) { @@ -37,14 +38,17 @@ export class ParseImageUtil implements IParseImage { editable: true, desiredSize: defaultSize }; - const b = new Downsamper(imageinfo, hValue, wValue, requ,request) - let options: image.DecodingOptions = { - editable: true, - desiredSize: { - height: b.targetHeight, - width: b.targetWidth - } - }; + if(request.downsampType.getName()!=='DownsampleNone'){ + const b:ESObject = new Downsampler().calculateScaling(imageinfo, hValue, wValue,request) + opts= { + editable: true, + desiredSize: { + height: b.targetHeight, + width: b.targetWidth + } + }; + } + imageSource.createPixelMap(opts).then((pixelMap: image.PixelMap) => {