降采样适配SVG图片
This commit is contained in:
parent
4a8794bbfd
commit
2a01e5ea4d
|
@ -5,6 +5,7 @@ import { FileTypeUtil } from '../utils/FileTypeUtil';
|
||||||
import { CenterOutside, FitCenter, SampleSizeRounding } from './DownsampleStartegy';
|
import { CenterOutside, FitCenter, SampleSizeRounding } from './DownsampleStartegy';
|
||||||
|
|
||||||
let TAG = 'downsampling'
|
let TAG = 'downsampling'
|
||||||
|
|
||||||
export class Downsampler {
|
export class Downsampler {
|
||||||
calculateScaling(
|
calculateScaling(
|
||||||
imageType: ArrayBuffer,
|
imageType: ArrayBuffer,
|
||||||
|
@ -50,8 +51,12 @@ export class Downsampler {
|
||||||
let widthScaleFactor = Math.floor(orientedSourceWidth / outWidth);
|
let widthScaleFactor = Math.floor(orientedSourceWidth / outWidth);
|
||||||
let heightScaleFactor = Math.floor(orientedSourceHeight / outHeight);
|
let heightScaleFactor = Math.floor(orientedSourceHeight / outHeight);
|
||||||
let scaleFactor = rounding == SampleSizeRounding.MEMORY ? Math.max(widthScaleFactor, heightScaleFactor) : Math.min(widthScaleFactor, heightScaleFactor)
|
let scaleFactor = rounding == SampleSizeRounding.MEMORY ? Math.max(widthScaleFactor, heightScaleFactor) : Math.min(widthScaleFactor, heightScaleFactor)
|
||||||
|
// 将整型的缩放因子转换为2的次幂采样大小
|
||||||
let powerOfTwoSampleSize: number;
|
let powerOfTwoSampleSize: number;
|
||||||
powerOfTwoSampleSize = Math.max(1, this.highestOneBit(scaleFactor));
|
if (rounding == SampleSizeRounding.MEMORY && powerOfTwoSampleSize < (1 / exactScaleFactor)) {
|
||||||
|
powerOfTwoSampleSize = powerOfTwoSampleSize << 1;
|
||||||
|
}
|
||||||
|
// 基于上一步得出的采样大小,根据不同的图片类型,计算采样后的图片尺寸
|
||||||
if (fileType == "JPEG") {
|
if (fileType == "JPEG") {
|
||||||
let nativeScaling = Math.min(powerOfTwoSampleSize, 8);
|
let nativeScaling = Math.min(powerOfTwoSampleSize, 8);
|
||||||
powerOfTwoWidth = Math.ceil(orientedSourceWidth / nativeScaling);
|
powerOfTwoWidth = Math.ceil(orientedSourceWidth / nativeScaling);
|
||||||
|
@ -95,6 +100,7 @@ export class Downsampler {
|
||||||
let a: ESObject = { "targetWidth": powerOfTwoWidth, "targetHeight": powerOfTwoHeight }
|
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) {
|
||||||
//if (!options.inJustDecodeBounds){
|
//if (!options.inJustDecodeBounds){
|
||||||
// callbacks.onObtainBounds();
|
// callbacks.onObtainBounds();
|
||||||
|
@ -117,12 +123,15 @@ export class Downsampler {
|
||||||
i |= (i >> 16);
|
i |= (i >> 16);
|
||||||
return i - (i >>> 1);
|
return i - (i >>> 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
round(value: number): number {
|
round(value: number): number {
|
||||||
return Math.floor(value + 0.5);
|
return Math.floor(value + 0.5);
|
||||||
}
|
}
|
||||||
|
|
||||||
isRotationRequired(degreesToRotate: number): boolean {
|
isRotationRequired(degreesToRotate: number): boolean {
|
||||||
return degreesToRotate == 90 || degreesToRotate == 270;
|
return degreesToRotate == 90 || degreesToRotate == 270;
|
||||||
}
|
}
|
||||||
|
|
||||||
// isScaling(options: ESObject): boolean {
|
// isScaling(options: ESObject): boolean {
|
||||||
// return options.inTargetDensity >0
|
// return options.inTargetDensity >0
|
||||||
// && options.inDensity>0
|
// && options.inDensity>0
|
||||||
|
@ -131,6 +140,7 @@ export class Downsampler {
|
||||||
getDensityMultiplier(adjustedScaleFactor: number): number {
|
getDensityMultiplier(adjustedScaleFactor: number): number {
|
||||||
return Math.round(Number.MAX_VALUE * (adjustedScaleFactor <= 1 ? adjustedScaleFactor : 1 / adjustedScaleFactor));
|
return Math.round(Number.MAX_VALUE * (adjustedScaleFactor <= 1 ? adjustedScaleFactor : 1 / adjustedScaleFactor));
|
||||||
}
|
}
|
||||||
|
|
||||||
adjustTargetDensityForError(adjustedScaleFactor: number): number {
|
adjustTargetDensityForError(adjustedScaleFactor: number): number {
|
||||||
let densityMultiplier = this.getDensityMultiplier(adjustedScaleFactor);
|
let densityMultiplier = this.getDensityMultiplier(adjustedScaleFactor);
|
||||||
let targetDensity = this.round(densityMultiplier * adjustedScaleFactor);
|
let targetDensity = this.round(densityMultiplier * adjustedScaleFactor);
|
||||||
|
|
|
@ -20,7 +20,12 @@ import { RequestOption } from '../RequestOption';
|
||||||
import { Downsampler } from '../Downsampling/Downsampler';
|
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
|
||||||
|
) {
|
||||||
this.parseImageThumbnail(1, imageinfo, onCompleteFunction, onErrorFunction,request)
|
this.parseImageThumbnail(1, imageinfo, onCompleteFunction, onErrorFunction,request)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,29 @@ export class SVGParseImpl implements IParseSvg {
|
||||||
editable: true,
|
editable: true,
|
||||||
desiredSize: defaultSize
|
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) => {
|
imageSource.createPixelMap(opts).then((pixelMap: image.PixelMap) => {
|
||||||
let imageKnifeData = ImageKnifeData.createImagePixelMap(ImageKnifeType.PIXELMAP, pixelMap)
|
let imageKnifeData = ImageKnifeData.createImagePixelMap(ImageKnifeType.PIXELMAP, pixelMap)
|
||||||
onComplete(imageKnifeData?.drawPixelMap?.imagePixelMap as PixelMap);
|
onComplete(imageKnifeData?.drawPixelMap?.imagePixelMap as PixelMap);
|
||||||
|
|
Loading…
Reference in New Issue