使用 @Sendable 装饰器修饰类,

This commit is contained in:
sijainguo 2024-04-27 17:36:31 +08:00
parent 9c40365fc4
commit 4a8794bbfd
7 changed files with 67 additions and 21 deletions

View File

@ -1,4 +1,7 @@
import {lang}from '@kit.ArkTs';
type ISendable=lang.ISendable;
export interface BaseDownsampling{ export interface BaseDownsampling{
getName():string
getScaleFactor(sourceWidth:number, sourceHeight:number, requestWidth:number, requestHeight:number):number getScaleFactor(sourceWidth:number, sourceHeight:number, requestWidth:number, requestHeight:number):number

View File

@ -1,7 +1,8 @@
import { BaseDownsampling } from './BaseDownsampling' import { BaseDownsampling } from './BaseDownsampling'
@Sendable
export class CenterInside{ export class CenterInside{
getName() { return "CenterInside"}
getScaleFactor(sourceWidth:number, sourceHeight:number,requestWidth:number, requestHeight:number):number { getScaleFactor(sourceWidth:number, sourceHeight:number,requestWidth:number, requestHeight:number):number {
return Math.min(1,FitCenter.getScaleFactor(sourceWidth, sourceHeight, requestWidth, requestHeight)) return Math.min(1,FitCenter.getScaleFactor(sourceWidth, sourceHeight, requestWidth, requestHeight))
@ -15,7 +16,11 @@ export class CenterInside{
} }
/*不进行下采样*/ /*不进行下采样*/
@Sendable
export class DownsampleNone implements BaseDownsampling{ export class DownsampleNone implements BaseDownsampling{
getName(){
return "DownsampleNone"
}
getScaleFactor(sourceWidth: number, sourceHeight: number, requestWidth: number, requestHeight: number): number { getScaleFactor(sourceWidth: number, sourceHeight: number, requestWidth: number, requestHeight: number): number {
return 1 return 1
} }
@ -26,7 +31,11 @@ export class DownsampleNone implements BaseDownsampling{
} }
/* 下采样使得图像的组大尺寸在给定的尺寸的1/2之间*/ /* 下采样使得图像的组大尺寸在给定的尺寸的1/2之间*/
@Sendable
export class AtMost implements BaseDownsampling{ export class AtMost implements BaseDownsampling{
getName(){
return "AtMost"
}
getScaleFactor(sourceWidth: number, sourceHeight: number, requestWidth:number,requestHeight: number): number { getScaleFactor(sourceWidth: number, sourceHeight: number, requestWidth:number,requestHeight: number): number {
let maxIntegerFactor=Math.ceil(Math.max(sourceHeight/requestHeight,sourceWidth/requestWidth)); let maxIntegerFactor=Math.ceil(Math.max(sourceHeight/requestHeight,sourceWidth/requestWidth));
let lesserOrEqualSampleSize = Math.max(1,highestOneBit(maxIntegerFactor)) let lesserOrEqualSampleSize = Math.max(1,highestOneBit(maxIntegerFactor))
@ -37,7 +46,12 @@ export class DownsampleNone implements BaseDownsampling{
return SampleSizeRounding.MEMORY return SampleSizeRounding.MEMORY
} }
} }
@Sendable
export class Atleast implements BaseDownsampling{ export class Atleast implements BaseDownsampling{
getName(){
return "Atleast"
}
getScaleFactor(sourceWidth: number, sourceHeight: number, requestWidth: number, requestHeight: number): number { getScaleFactor(sourceWidth: number, sourceHeight: number, requestWidth: number, requestHeight: number): number {
let minIntegerFactor=Math.floor(Math.min(sourceHeight/requestHeight,sourceWidth/requestWidth)) let minIntegerFactor=Math.floor(Math.min(sourceHeight/requestHeight,sourceWidth/requestWidth))
@ -47,7 +61,12 @@ export class Atleast implements BaseDownsampling{
return SampleSizeRounding.QUALITY return SampleSizeRounding.QUALITY
} }
} }
@Sendable
export class CenterOutside implements BaseDownsampling{ export class CenterOutside implements BaseDownsampling{
getName(){
return "CenterOutside"
}
getScaleFactor(sourceWidth: number, sourceHeight: number, requestWidth: number, requestHeight: number): number { getScaleFactor(sourceWidth: number, sourceHeight: number, requestWidth: number, requestHeight: number): number {
let widthPercentage =requestWidth/sourceWidth let widthPercentage =requestWidth/sourceWidth
let heightPercentage =requestHeight/sourceHeight let heightPercentage =requestHeight/sourceHeight
@ -60,8 +79,11 @@ export class CenterOutside implements BaseDownsampling{
} }
@Sendable
export class FitCenter{ export class FitCenter{
getName(){
return "FitCenter"
}
public static getScaleFactor(sourceWidth: number, sourceHeight: number, requestWidth: number, requestHeight: number): number { public static getScaleFactor(sourceWidth: number, sourceHeight: number, requestWidth: number, requestHeight: number): number {
let widthPercentage =requestWidth/sourceWidth let widthPercentage =requestWidth/sourceWidth
let heightPercentage =requestHeight/sourceHeight let heightPercentage =requestHeight/sourceHeight

View File

@ -1,8 +1,9 @@
/* /*
* asdfasdfasdfasdfasdf*/ * asdfasdfasdfasdfasdf*/
import { CenterOutside, FitCenter, SampleSizeRounding } from './Downsamplestrategy'; import { RequestOption } from '../RequestOption';
import { FileTypeUtil } from '../../utits/Fitetypeutit'; import { FileTypeUtil } from '../utils/FileTypeUtil';
import { RequestOption } from '../../Requestoption'; import { CenterOutside, FitCenter, SampleSizeRounding } from './DownsampleStartegy';
let TAG = 'downsampling' let TAG = 'downsampling'
export class Downsampler { export class Downsampler {
calculateScaling( calculateScaling(
@ -26,7 +27,7 @@ export class Downsampler {
} else { } else {
console.log('宽高都不存在') console.log('宽高都不存在')
} }
if (sourceWidth <= 0 || sourceleight <= 0) return; if (sourceWidth <= 0 || sourceHeight <= 0) return;
let orientedSourceWidth = sourceWidth; let orientedSourceWidth = sourceWidth;
let orientedSourceHeight = sourceHeight; let orientedSourceHeight = sourceHeight;
if (this.isRotationRequired(90)) { if (this.isRotationRequired(90)) {
@ -52,7 +53,7 @@ export class Downsampler {
let powerOfTwoSampleSize: number; let powerOfTwoSampleSize: number;
powerOfTwoSampleSize = Math.max(1, this.highestOneBit(scaleFactor)); powerOfTwoSampleSize = Math.max(1, this.highestOneBit(scaleFactor));
if (fileType == "JPEG") { if (fileType == "JPEG") {
let nativeScaling = Hath.min(powerOfTwoSampleSize, 8); let nativeScaling = Math.min(powerOfTwoSampleSize, 8);
powerOfTwoWidth = Math.ceil(orientedSourceWidth / nativeScaling); powerOfTwoWidth = Math.ceil(orientedSourceWidth / nativeScaling);
powerOfTwoHeight = Math.ceil(orientedSourceHeight / nativeScaling); powerOfTwoHeight = Math.ceil(orientedSourceHeight / nativeScaling);
let secondaryScaling = Math.floor(powerOfTwoSampleSize / 8); let secondaryScaling = Math.floor(powerOfTwoSampleSize / 8);
@ -62,11 +63,11 @@ export class Downsampler {
} }
} else if (fileType == "PNG") { } else if (fileType == "PNG") {
powerOfTwoWidth = Math.floor(orientedSourceWidth / powerOfTwoSampleSize); powerOfTwoWidth = Math.floor(orientedSourceWidth / powerOfTwoSampleSize);
PowerOfTwoHeight = Math.floor(orientedSourceHeight / powerOfTwoSamplesize); powerOfTwoHeight = Math.floor(orientedSourceHeight / powerOfTwoSampleSize);
console.log('执行了没', powerOfTwoHeight, powerOfTwoWidth) console.log('执行了没', powerOfTwoHeight, powerOfTwoWidth)
} else if (fileType == "WEBP") { } else if (fileType == "WEBP") {
powerOfTwoWidth = Math.round(orientedSourceWidth / powerOfTwoSampleSize); powerOfTwoWidth = Math.round(orientedSourceWidth / powerOfTwoSampleSize);
poWerOfTwoHeight = Math.round(orientedSourceHeight / powerOfTwoSampleSize); powerOfTwoHeight = Math.round(orientedSourceHeight / powerOfTwoSampleSize);
} else if ( } else if (
orientedSourceWidth % powerOfTwoSampleSize != 0 || orientedSourceHeight % powerOfTwoSampleSize != 0) { orientedSourceWidth % powerOfTwoSampleSize != 0 || orientedSourceHeight % powerOfTwoSampleSize != 0) {
@ -91,7 +92,7 @@ export class Downsampler {
// options.inDensity = options.inTargetDensity =0; // options.inDensity = options.inTargetDensity =0;
// } // }
//} //}
let a: ESObject = { "targetWidth": power0fTwoWidth, "targetHeight": powerOfTuoHeight } let a: ESObject = { "targetWidth": powerOfTwoWidth, "targetHeight": powerOfTwoHeight }
return a return a
} }
//decodeStream(imageReader:ImageReader, options: ESObject, callbacks: DecodeCallbacks, bitmapPool: ESObject) { //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 >> 1);
i |= (i >> 2); i |= (i >> 2);
i |= (i >> 4); i |= (i >> 4);

View File

@ -563,6 +563,7 @@ export class ImageKnife {
data.setDiskMemoryCachePath(this.diskMemoryCache.getPath()) data.setDiskMemoryCachePath(this.diskMemoryCache.getPath())
data.setPlaceHolderRegisterCacheKey(request.placeholderRegisterCacheKey); data.setPlaceHolderRegisterCacheKey(request.placeholderRegisterCacheKey);
data.setPlaceHolderRegisterMemoryCacheKey(request.placeholderRegisterMemoryCacheKey); data.setPlaceHolderRegisterMemoryCacheKey(request.placeholderRegisterMemoryCacheKey);
data.setDownsampType(request.downsampType)
return data; return data;
} }
@ -811,7 +812,7 @@ async function taskExecute(sendData:SendableData,taskData:TaskParams): Promise<P
newRequestOption.size = taskData.size; newRequestOption.size = taskData.size;
newRequestOption.placeholderRegisterCacheKey = sendData.getPlaceHolderRegisterCacheKey(); newRequestOption.placeholderRegisterCacheKey = sendData.getPlaceHolderRegisterCacheKey();
newRequestOption.placeholderRegisterMemoryCacheKey = sendData.getPlaceHolderRegisterMemoryCacheKey(); newRequestOption.placeholderRegisterMemoryCacheKey = sendData.getPlaceHolderRegisterMemoryCacheKey();
newRequestOption.downsampType = sendData.getDownsampType();
if(taskData.placeholderSrc){ if(taskData.placeholderSrc){
newRequestOption.placeholderSrc = taskData.placeholderSrc as string | PixelMap | Resource | undefined; newRequestOption.placeholderSrc = taskData.placeholderSrc as string | PixelMap | Resource | undefined;
} }

View File

@ -28,6 +28,8 @@ import { ObjectKey } from './ObjectKey'
import common from '@ohos.app.ability.common' import common from '@ohos.app.ability.common'
import { Priority } from './RequestOption' import { Priority } from './RequestOption'
import { DataFetchResult } from './networkmanage/DataFetchResult' import { DataFetchResult } from './networkmanage/DataFetchResult'
import { BaseDownsampling } from './Downsampling/BaseDownsampling'
import { DownsampleNone } from './Downsampling/DownsampleStartegy'
export interface CropCircleWithBorder{ export interface CropCircleWithBorder{
border: number, border: number,
@ -87,6 +89,9 @@ export class ImageKnifeOption {
// gif加载展示一帧 // gif加载展示一帧
dontAnimateFlag? = false; dontAnimateFlag? = false;
//下采样
downsampling?:BaseDownsampling = new DownsampleNone()
// 占位图 // 占位图
placeholderSrc?: string | PixelMap | Resource; placeholderSrc?: string | PixelMap | Resource;
placeholderScaleType?: ScaleType = ScaleType.FIT_CENTER placeholderScaleType?: ScaleType = ScaleType.FIT_CENTER

View File

@ -18,6 +18,8 @@ import { collections } from '@kit.ArkTS';
import { IDataFetch } from './networkmanage/IDataFetch'; import { IDataFetch } from './networkmanage/IDataFetch';
import { DiskLruCache } from '../cache/DiskLruCache'; import { DiskLruCache } from '../cache/DiskLruCache';
import { DownloadClient } from './networkmanage/DownloadClient'; import { DownloadClient } from './networkmanage/DownloadClient';
import { BaseDownsampling } from './Downsampling/BaseDownsampling';
import { DownsampleNone } from './Downsampling/DownsampleStartegy';
@Sendable @Sendable
export class SendableData{ export class SendableData{
@ -41,7 +43,7 @@ export class SendableData{
private dataFetch: IDataFetch = new DownloadClient(); private dataFetch: IDataFetch = new DownloadClient();
private placeholderRegisterCacheKey: string = ""; private placeholderRegisterCacheKey: string = "";
private placeholderRegisterMemoryCacheKey: string = ""; private placeholderRegisterMemoryCacheKey: string = "";
private downsampType: BaseDownsampling = new DownsampleNone();
public setDataFetch(value: IDataFetch) { public setDataFetch(value: IDataFetch) {
this.dataFetch = value; this.dataFetch = value;
} }
@ -194,4 +196,12 @@ export class SendableData{
public getPlaceHolderRegisterMemoryCacheKey(): string { public getPlaceHolderRegisterMemoryCacheKey(): string {
return this.placeholderRegisterMemoryCacheKey; return this.placeholderRegisterMemoryCacheKey;
} }
public setDownsampType(Type: BaseDownsampling) {
this.downsampType = Type;
}
public getDownsampType(): BaseDownsampling {
return this.downsampType;
}
} }

View File

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