使用 @Sendable 装饰器修饰类,
This commit is contained in:
parent
9c40365fc4
commit
4a8794bbfd
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<P
|
|||
newRequestOption.size = taskData.size;
|
||||
newRequestOption.placeholderRegisterCacheKey = sendData.getPlaceHolderRegisterCacheKey();
|
||||
newRequestOption.placeholderRegisterMemoryCacheKey = sendData.getPlaceHolderRegisterMemoryCacheKey();
|
||||
|
||||
newRequestOption.downsampType = sendData.getDownsampType();
|
||||
if(taskData.placeholderSrc){
|
||||
newRequestOption.placeholderSrc = taskData.placeholderSrc as string | PixelMap | Resource | undefined;
|
||||
}
|
||||
|
|
|
@ -28,6 +28,8 @@ import { ObjectKey } from './ObjectKey'
|
|||
import common from '@ohos.app.ability.common'
|
||||
import { Priority } from './RequestOption'
|
||||
import { DataFetchResult } from './networkmanage/DataFetchResult'
|
||||
import { BaseDownsampling } from './Downsampling/BaseDownsampling'
|
||||
import { DownsampleNone } from './Downsampling/DownsampleStartegy'
|
||||
|
||||
export interface CropCircleWithBorder{
|
||||
border: number,
|
||||
|
@ -87,6 +89,9 @@ export class ImageKnifeOption {
|
|||
// gif加载展示一帧
|
||||
dontAnimateFlag? = false;
|
||||
|
||||
//下采样
|
||||
downsampling?:BaseDownsampling = new DownsampleNone()
|
||||
|
||||
// 占位图
|
||||
placeholderSrc?: string | PixelMap | Resource;
|
||||
placeholderScaleType?: ScaleType = ScaleType.FIT_CENTER
|
||||
|
|
|
@ -18,6 +18,8 @@ import { collections } from '@kit.ArkTS';
|
|||
import { IDataFetch } from './networkmanage/IDataFetch';
|
||||
import { DiskLruCache } from '../cache/DiskLruCache';
|
||||
import { DownloadClient } from './networkmanage/DownloadClient';
|
||||
import { BaseDownsampling } from './Downsampling/BaseDownsampling';
|
||||
import { DownsampleNone } from './Downsampling/DownsampleStartegy';
|
||||
|
||||
@Sendable
|
||||
export class SendableData{
|
||||
|
@ -41,7 +43,7 @@ export class SendableData{
|
|||
private dataFetch: IDataFetch = new DownloadClient();
|
||||
private placeholderRegisterCacheKey: string = "";
|
||||
private placeholderRegisterMemoryCacheKey: string = "";
|
||||
|
||||
private downsampType: BaseDownsampling = new DownsampleNone();
|
||||
public setDataFetch(value: IDataFetch) {
|
||||
this.dataFetch = value;
|
||||
}
|
||||
|
@ -194,4 +196,12 @@ export class SendableData{
|
|||
public getPlaceHolderRegisterMemoryCacheKey(): string {
|
||||
return this.placeholderRegisterMemoryCacheKey;
|
||||
}
|
||||
|
||||
public setDownsampType(Type: BaseDownsampling) {
|
||||
this.downsampType = Type;
|
||||
}
|
||||
|
||||
public getDownsampType(): BaseDownsampling {
|
||||
return this.downsampType;
|
||||
}
|
||||
}
|
|
@ -17,6 +17,7 @@ import { IParseImage } from '../interface/IParseImage'
|
|||
import image from '@ohos.multimedia.image';
|
||||
import { BusinessError } from '@ohos.base'
|
||||
import { RequestOption } from '../RequestOption';
|
||||
import { Downsampler } from '../Downsampling/Downsampler';
|
||||
|
||||
export class ParseImageUtil implements IParseImage<PixelMap> {
|
||||
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,
|
||||
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) => {
|
||||
|
|
Loading…
Reference in New Issue