Compare commits
8 Commits
c9957c458f
...
b0e0f97022
Author | SHA1 | Date |
---|---|---|
|
b0e0f97022 | |
|
122c01a166 | |
|
e79557cf7c | |
|
d2646b5ad4 | |
|
c79046f005 | |
|
5a9b251e87 | |
|
647b2dc1f8 | |
|
ba95318b57 |
|
@ -126,16 +126,20 @@ struct DownSamplePage {
|
|||
desiredPixelFormat: 3,
|
||||
}
|
||||
let imageInfo = await imageSource.getImageInfo()
|
||||
let reqSize =
|
||||
new Downsampler().calculateScaling(typeValue, imageInfo.size.width, imageInfo.size.height, 300,
|
||||
300, this.imageKnifeOption.downsampleOf)
|
||||
decodingOptions = {
|
||||
editable: true,
|
||||
desiredSize: {
|
||||
width: reqSize.width,
|
||||
height: reqSize.height
|
||||
|
||||
if (this.imageKnifeOption.downsampleOf !== DownsampleStrategy.NONE){
|
||||
let reqSize =
|
||||
new Downsampler().calculateScaling(typeValue, imageInfo.size.width, imageInfo.size.height, 300,
|
||||
300, this.imageKnifeOption.downsampleOf)
|
||||
decodingOptions = {
|
||||
editable: true,
|
||||
desiredSize: {
|
||||
width: reqSize.width,
|
||||
height: reqSize.height
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 创建pixelMap
|
||||
imageSource.createPixelMap(decodingOptions).then((pixelMap: image.PixelMap) => {
|
||||
this.afterSampling = pixelMap.getPixelBytesNumber()
|
||||
|
|
|
@ -40,19 +40,9 @@ struct Index {
|
|||
uri: 'pages/TestCommonImage',
|
||||
});
|
||||
})
|
||||
Button($r('app.string.Image_Downsampling_Functionality')).margin({top:10}).onClick(()=>{
|
||||
Button(this.getResourceString($r('app.string.Multiple_images')) + " + reuse + LazyForeach").margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/DownSamplePage',
|
||||
});
|
||||
})
|
||||
Button($r('app.string.Test_Task_error')).margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/TestTaskResourcePage',
|
||||
});
|
||||
})
|
||||
Button($r('app.string.Test_HSP')).margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/TestHspPreLoadImage',
|
||||
uri: 'pages/UserPage',
|
||||
|
||||
});
|
||||
})
|
||||
|
@ -62,22 +52,9 @@ struct Index {
|
|||
|
||||
});
|
||||
})
|
||||
Button($r('app.string.Test_custom_download')).margin({top:10}).onClick(()=>{
|
||||
Button($r('app.string.Image_Downsampling_Functionality')).margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/TestSetCustomImagePage',
|
||||
|
||||
});
|
||||
})
|
||||
Button(this.getResourceString($r('app.string.Multiple_images')) + " + LazyForEach").margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/ManyPhotoShowPage',
|
||||
|
||||
});
|
||||
})
|
||||
Button(this.getResourceString($r('app.string.Multiple_images')) + " + reuse + LazyForeach").margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/UserPage',
|
||||
|
||||
uri: 'pages/DownSamplePage',
|
||||
});
|
||||
})
|
||||
Button($r('app.string.Display_long_image')).margin({top:10}).onClick(()=>{
|
||||
|
@ -86,17 +63,25 @@ struct Index {
|
|||
|
||||
});
|
||||
})
|
||||
Button($r('app.string.Image_scaling')).margin({top:10}).onClick(()=>{
|
||||
Button($r('app.string.Image_Transformation')).margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/TransformPage',
|
||||
uri: 'pages/ImageTransformation',
|
||||
|
||||
});
|
||||
|
||||
})
|
||||
Button($r('app.string.Test_media_URL')).margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/dataShareUriLoadPage',
|
||||
|
||||
});
|
||||
})
|
||||
Button(this.getResourceString($r('app.string.Message_list')) + " + List").margin({top:10}).onClick(()=>{
|
||||
Button($r('app.string.Different_ObjectFit')).margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/TestImageFlash',
|
||||
uri: 'pages/ObjectFitPage',
|
||||
|
||||
});
|
||||
|
||||
})
|
||||
Button($r('app.string.Custom_cache_key')).margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
|
@ -104,6 +89,39 @@ struct Index {
|
|||
|
||||
});
|
||||
})
|
||||
Button($r('app.string.Test_image_loading_success_or_failure_events')).margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/LoadStatePage',
|
||||
})
|
||||
})
|
||||
Button($r('app.string.Image_scaling')).margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/TransformPage',
|
||||
|
||||
});
|
||||
})
|
||||
|
||||
Button($r('app.string.Test_HSP')).margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/TestHspPreLoadImage',
|
||||
|
||||
});
|
||||
})
|
||||
|
||||
Button($r('app.string.Test_custom_download')).margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/TestSetCustomImagePage',
|
||||
|
||||
});
|
||||
})
|
||||
|
||||
Button(this.getResourceString($r('app.string.Message_list')) + " + List").margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/TestImageFlash',
|
||||
|
||||
});
|
||||
})
|
||||
|
||||
Button($r('app.string.Preloading_images_to_cache')).margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/TestPrefetchToFileCache',
|
||||
|
@ -128,27 +146,7 @@ struct Index {
|
|||
|
||||
});
|
||||
})
|
||||
Button($r('app.string.Image_Transformation')).margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/ImageTransformation',
|
||||
|
||||
});
|
||||
|
||||
})
|
||||
|
||||
Button($r('app.string.Different_ObjectFit')).margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/ObjectFitPage',
|
||||
|
||||
});
|
||||
|
||||
})
|
||||
|
||||
Button($r('app.string.Test_image_loading_success_or_failure_events')).margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/LoadStatePage',
|
||||
})
|
||||
})
|
||||
|
||||
Button($r('app.string.Test_removing_image_cache_interface')).margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
|
@ -161,13 +159,11 @@ struct Index {
|
|||
|
||||
});
|
||||
})
|
||||
Button($r('app.string.Test_media_URL')).margin({top:10}).onClick(()=>{
|
||||
Button($r('app.string.Test_Task_error')).margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/dataShareUriLoadPage',
|
||||
|
||||
uri: 'pages/TestTaskResourcePage',
|
||||
});
|
||||
})
|
||||
|
||||
}
|
||||
} .width('100%')
|
||||
.height('100%')
|
||||
|
|
|
@ -388,28 +388,28 @@ export class ImageKnifeLoader {
|
|||
return resBuf
|
||||
}
|
||||
|
||||
static downsamplerReqSize(typeValue: string, request: RequestJobRequest, size: Size,
|
||||
SRC?: ImageKnifeRequestSource) {
|
||||
static downsamplerReqSize(typeValue: string, request: RequestJobRequest, size: Size,
|
||||
SRC?: ImageKnifeRequestSource):image.DecodingOptions {
|
||||
let reqSize =
|
||||
new Downsampler().calculateScaling(typeValue, size.width, size.height, request.targetWidth, request.targetHeight,
|
||||
request.downsampType)
|
||||
if (typeValue == "svg") {
|
||||
return ({
|
||||
return {
|
||||
editable: true,
|
||||
desiredSize: ({
|
||||
desiredSize: {
|
||||
height: vp2px(reqSize.height),
|
||||
width: vp2px(reqSize.width)
|
||||
} as Size)
|
||||
}
|
||||
|
||||
} as image.DecodingOptions)
|
||||
}
|
||||
} else {
|
||||
return ({
|
||||
return {
|
||||
editable: request.requestSource === SRC && request.transformation !== undefined ? true : false,
|
||||
desiredSize: ({
|
||||
desiredSize:{
|
||||
width: reqSize.width,
|
||||
height: reqSize.height
|
||||
} as Size)
|
||||
} as image.DecodingOptions)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,4 @@ export interface BaseDownsampling {
|
|||
getName(): string
|
||||
|
||||
getScaleFactor(sourceWidth: number, sourceHeight: number, requestWidth: number, requestHeight: number,downsampType?:DownsampleStrategy): number
|
||||
//
|
||||
getSampleSizeType(sourceWidth: number, sourceHeight: number, requestWidth: number,
|
||||
requestHeight: number,downsampType?:DownsampleStrategy): SampleSizeRounding
|
||||
}
|
|
@ -28,50 +28,16 @@ export class FitCenter implements BaseDownsampling {
|
|||
width: round(getScale(sourceWidth, sourceHeight, requestedWidth, requestedHeight, downsampType) * sourceWidth),
|
||||
height:round(getScale(sourceWidth, sourceHeight, requestedWidth, requestedHeight, downsampType) * sourceHeight)
|
||||
}
|
||||
let scaleFactor = this.getSampleSizeType(sourceWidth, sourceHeight, requestedWidth, requestedHeight, downsampType)===
|
||||
SampleSizeRounding.QUALITY? Math.max(1, highestOneBit(Math.max(sourceWidth / outSize.width, sourceHeight / outSize.height))) :
|
||||
let scaleFactor = downsampType === DownsampleStrategy.FIT_CENTER_QUALITY?
|
||||
Math.max(1, highestOneBit(Math.max(sourceWidth / outSize.width, sourceHeight / outSize.height))) :
|
||||
Math.max(1, highestOneBit(Math.min(sourceWidth / outSize.width, sourceHeight / outSize.height)))//将整型的缩放因子转换为2的次幂采样大小
|
||||
|
||||
if (this.getSampleSizeType(sourceWidth, sourceHeight, requestedWidth, requestedHeight, downsampType)===
|
||||
SampleSizeRounding.MEMORY && (scaleFactor < (1 / getScale(sourceWidth, sourceHeight, requestedWidth, requestedHeight, downsampType)))) {
|
||||
if (downsampType === DownsampleStrategy.FIT_CENTER_MEMORY
|
||||
&& (scaleFactor < (1 / getScale(sourceWidth, sourceHeight, requestedWidth, requestedHeight, downsampType)))) {
|
||||
scaleFactor = scaleFactor << 1;
|
||||
}
|
||||
return scaleFactor
|
||||
}
|
||||
|
||||
getSampleSizeType(sourceWidth: number, sourceHeight: number, requestedWidth: number, requestedHeight: number,
|
||||
downsampType: DownsampleStrategy
|
||||
): number {
|
||||
if (downsampType === DownsampleStrategy.FIT_CENTER_QUALITY) {
|
||||
return SampleSizeRounding.QUALITY;
|
||||
} else {
|
||||
return SampleSizeRounding.MEMORY;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export class None implements BaseDownsampling {
|
||||
getName(): string {
|
||||
return "DownsampleNone"
|
||||
}
|
||||
|
||||
public getScaleFactor(sourceWidth: number, sourceHeight: number, requestedWidth: number,
|
||||
requestedHeight: number): number {
|
||||
let outSize: Size = {
|
||||
width: round(sourceWidth),
|
||||
height:round(sourceHeight)
|
||||
}
|
||||
let scaleFactor =
|
||||
Math.max(1, highestOneBit(Math.max(sourceWidth / outSize.width, sourceHeight / outSize.height)))
|
||||
return scaleFactor
|
||||
}
|
||||
|
||||
//实现 getSampleSizeType 方法
|
||||
public getSampleSizeType(sourceWidth: number, sourceHeight: number, requestedWidth: number,
|
||||
requestedHeight: number): SampleSizeRounding {
|
||||
return SampleSizeRounding.QUALITY
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -95,11 +61,6 @@ export class CenterOutside implements BaseDownsampling {
|
|||
|
||||
return scaleFactor
|
||||
}
|
||||
|
||||
getSampleSizeType(sourceWidth: number, sourceHeight: number, requestedWidth: number,
|
||||
requestedHeight: number): SampleSizeRounding {
|
||||
return SampleSizeRounding.QUALITY;
|
||||
}
|
||||
}
|
||||
|
||||
/*请求尺寸大于实际尺寸不进行放大,按照原图展示*/
|
||||
|
@ -118,7 +79,7 @@ export class AtMost implements BaseDownsampling {
|
|||
greaterOrEqualSampleSize = lesserOrEqualSampleSize <<= 1;
|
||||
}
|
||||
greaterOrEqualSampleSize = lesserOrEqualSampleSize << (lesserOrEqualSampleSize < maxIntegerFactor ? 1 : 0)
|
||||
//返回缩放因子
|
||||
|
||||
let outSize: Size = {
|
||||
width: round((1 / greaterOrEqualSampleSize) * sourceWidth),
|
||||
height:round((1 / greaterOrEqualSampleSize) * sourceHeight)
|
||||
|
@ -129,13 +90,6 @@ export class AtMost implements BaseDownsampling {
|
|||
}
|
||||
return scaleFactor
|
||||
}
|
||||
|
||||
|
||||
getSampleSizeType(sourceWidth: number, sourceHeight: number, requestedWidth: number,
|
||||
requestHeight: number): SampleSizeRounding {
|
||||
//根据 AtMost 的逻辑,总是返回 MEMORY
|
||||
return SampleSizeRounding.MEMORY
|
||||
}
|
||||
}
|
||||
|
||||
/*宽高进行等比缩放宽高里面最大的比例先放进去
|
||||
|
@ -147,7 +101,6 @@ export class CenterInside implements BaseDownsampling {
|
|||
getScaleFactor(sourceWidth: number, sourceHeight: number, requestedWidth: number, requestedHeight: number,
|
||||
downsampType: DownsampleStrategy
|
||||
): number {
|
||||
|
||||
let outSize: Size = {
|
||||
width: round(Math.min(1, getScale(sourceWidth, sourceHeight, requestedWidth, requestedHeight, downsampType)) * sourceWidth),
|
||||
height:round(Math.min(1, getScale(sourceWidth, sourceHeight, requestedWidth, requestedHeight, downsampType)) * sourceHeight)
|
||||
|
@ -173,17 +126,7 @@ export class CenterInside implements BaseDownsampling {
|
|||
return SampleSizeRounding.QUALITY
|
||||
}
|
||||
//否则,使用 FIL_CENTER 的 SampleSizeRounding 值
|
||||
return this.getSampleSize(sourceWidth, sourceHeight, requestedWidth, requestedHeight, downsampType);
|
||||
}
|
||||
|
||||
getSampleSize(sourceWidth: number, sourceHeight: number, requestedWidth: number, requestedHeight: number,
|
||||
downsampType: DownsampleStrategy
|
||||
): SampleSizeRounding {
|
||||
if (downsampType === DownsampleStrategy.CENTER_INSIDE_MEMORY) {
|
||||
return SampleSizeRounding.MEMORY;
|
||||
} else {
|
||||
return SampleSizeRounding.QUALITY;
|
||||
}
|
||||
return downsampType === DownsampleStrategy.CENTER_INSIDE_MEMORY?SampleSizeRounding.MEMORY:SampleSizeRounding.QUALITY
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ export enum SampleSizeRounding {
|
|||
//(质量优先)
|
||||
QUALITY
|
||||
}
|
||||
|
||||
//找出给定整数 i 中最高位的1(即最左边的1)所代表的值
|
||||
export function highestOneBit(i: number): number {
|
||||
i |= (i >> 1);
|
||||
i |= (i >> 2);
|
||||
|
@ -52,6 +52,7 @@ export function getScale(sourceWidth: number, sourceHeight: number, requestedWid
|
|||
|
||||
}
|
||||
}
|
||||
//四舍五入
|
||||
export function round(value: number): number {
|
||||
return Math.floor(value + 0.5);
|
||||
}
|
|
@ -18,11 +18,7 @@ import {
|
|||
CenterOutside,
|
||||
DownsampleStrategy,
|
||||
FitCenter,
|
||||
None,
|
||||
} from './DownsampleStartegy';
|
||||
import { highestOneBit, round, SampleSizeRounding } from './DownsampleUtils';
|
||||
|
||||
|
||||
export class Downsampler {
|
||||
calculateScaling(
|
||||
typeValue: string,
|
||||
|
@ -39,22 +35,6 @@ export class Downsampler {
|
|||
let downsampler = this.getDownsampler(downsampType);
|
||||
let scaleFactor: number =
|
||||
downsampler.getScaleFactor(sourceWidth, sourceHeight, requestWidth, requestHeight, downsampType);//缩放比
|
||||
// let rounding: SampleSizeRounding =
|
||||
// downsampler.getSampleSizeType(sourceWidth, sourceHeight, requestWidth, requestHeight, downsampType); //采样类型
|
||||
//根据缩放比重新计算宽高
|
||||
// let outSize: Size = {
|
||||
// width: round(exactScaleFactor * sourceWidth),
|
||||
// height:round(exactScaleFactor * sourceHeight)
|
||||
// }
|
||||
// let scaleFactor =
|
||||
// rounding == SampleSizeRounding.QUALITY ? Math.max(sourceWidth / exactScaleFactor.width, sourceHeight / exactScaleFactor.height) :
|
||||
// Math.min(sourceWidth / exactScaleFactor.width, sourceHeight / exactScaleFactor.height) //重新计算宽高比
|
||||
|
||||
// scaleFactor = Math.max(1, highestOneBit(scaleFactor))//将整型的缩放因子转换为2的次幂采样大小
|
||||
//
|
||||
// if (rounding == 0 && (scaleFactor < (1 / exactScaleFactor))) {
|
||||
// scaleFactor = scaleFactor << 1;
|
||||
// }
|
||||
//基于上一步得出的采样大小,根据不同的图片类型,计算采样后的图片尺寸
|
||||
if (typeValue === "png") {
|
||||
return {
|
||||
|
@ -74,14 +54,11 @@ export class Downsampler {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
getDownsampler(downsampType: DownsampleStrategy) {
|
||||
switch (downsampType) {
|
||||
case DownsampleStrategy.FIT_CENTER_MEMORY:
|
||||
case DownsampleStrategy.FIT_CENTER_QUALITY:
|
||||
return new FitCenter();
|
||||
case DownsampleStrategy.NONE:
|
||||
return new None();
|
||||
case DownsampleStrategy.AT_MOST:
|
||||
return new AtMost();
|
||||
case DownsampleStrategy.CENTER_INSIDE_MEMORY:
|
||||
|
|
Loading…
Reference in New Issue