@@ -28,7 +28,7 @@ export default class MediaProcessor {
2828 * @param inputs
2929 */
3030 process ( media , modelConfig , feedShape ) : InputFeed [ ] {
31- const { fill, mean, std, bgr } = modelConfig ;
31+ const { fill, mean, std, bgr, keepRatio = true } = modelConfig ;
3232 const { fc = 3 , fh, fw } = feedShape ;
3333 const input = media ;
3434
@@ -38,6 +38,7 @@ export default class MediaProcessor {
3838 mean : mean || this . mean ,
3939 std : std || this . std ,
4040 bgr : bgr || this . bgr ,
41+ keepRatio,
4142 targetSize : {
4243 width : fw ,
4344 height : fh
@@ -75,7 +76,7 @@ export default class MediaProcessor {
7576 this . pixelHeight = ( pixels as HTMLImageElement ) . naturalHeight || pixels . height ;
7677
7778 const inGPU = env . get ( 'webgl_gpu_pipeline' ) || opt . webglFeedProcess ;
78- this . fitToTargetSize ( isImageElementLike ? input . path : input , imageDataInfo , inGPU ) ;
79+ this . fitToTargetSize ( isImageElementLike ? input . path : input , imageDataInfo , opt . keepRatio , inGPU ) ;
7980 data = this . getImageData ( imageDataInfo ) ;
8081 // process imageData in webgl
8182 if ( inGPU ) {
@@ -133,9 +134,9 @@ export default class MediaProcessor {
133134
134135
135136 /**
136- * 缩放成目标尺寸并居中
137+ * 缩放成目标尺寸, keepRatio 为 true 则保持比例拉伸并居中,为 false 则变形拉伸为目标尺寸
137138 */
138- fitToTargetSize ( image , imageDataInfo , inGPU = false ) {
139+ fitToTargetSize ( image , imageDataInfo , keepRatio = true , inGPU = false ) {
139140 // 目标尺寸
140141 const targetWidth = imageDataInfo . dWidth ;
141142 const targetHeight = imageDataInfo . dHeight ;
@@ -147,26 +148,28 @@ export default class MediaProcessor {
147148 let sh = inGPU ? this . pixelHeight : targetHeight ;
148149 let x = 0 ;
149150 let y = 0 ;
150- // target的长宽比大些 就把原图的高变成target那么高
151- if ( targetWidth / targetHeight * this . pixelHeight / this . pixelWidth >= 1 ) {
152- if ( inGPU ) {
153- canvasWidth = Math . round ( sh * targetWidth / targetHeight ) ;
154- x = Math . floor ( ( canvasWidth - sw ) / 2 ) ;
155- }
156- else {
157- sw = Math . round ( sh * this . pixelWidth / this . pixelHeight ) ;
158- x = Math . floor ( ( targetWidth - sw ) / 2 ) ;
159- }
160- }
161- // target的长宽比小些 就把原图的宽变成target那么宽
162- else {
163- if ( inGPU ) {
164- canvasHeight = Math . round ( sw * targetHeight / targetWidth ) ;
165- y = Math . floor ( ( canvasHeight - sh ) / 2 ) ;
151+ if ( keepRatio ) {
152+ // target的长宽比大些 就把原图的高变成target那么高
153+ if ( targetWidth / targetHeight * this . pixelHeight / this . pixelWidth >= 1 ) {
154+ if ( inGPU ) {
155+ canvasWidth = Math . round ( sh * targetWidth / targetHeight ) ;
156+ x = Math . floor ( ( canvasWidth - sw ) / 2 ) ;
157+ }
158+ else {
159+ sw = Math . round ( sh * this . pixelWidth / this . pixelHeight ) ;
160+ x = Math . floor ( ( targetWidth - sw ) / 2 ) ;
161+ }
166162 }
163+ // target的长宽比小些 就把原图的宽变成target那么宽
167164 else {
168- sh = Math . round ( sw * this . pixelHeight / this . pixelWidth ) ;
169- y = Math . floor ( ( targetHeight - sh ) / 2 ) ;
165+ if ( inGPU ) {
166+ canvasHeight = Math . round ( sw * targetHeight / targetWidth ) ;
167+ y = Math . floor ( ( canvasHeight - sh ) / 2 ) ;
168+ }
169+ else {
170+ sh = Math . round ( sw * this . pixelHeight / this . pixelWidth ) ;
171+ y = Math . floor ( ( targetHeight - sh ) / 2 ) ;
172+ }
170173 }
171174 }
172175
@@ -175,7 +178,7 @@ export default class MediaProcessor {
175178 this . targetCanvas . width = canvasWidth ;
176179 this . targetCanvas . height = canvasHeight ;
177180 this . targetContext . fillStyle = imageDataInfo . gapFillWith ;
178- this . targetContext . fillRect ( 0 , 0 , canvasHeight , canvasWidth ) ;
181+ this . targetContext . fillRect ( 0 , 0 , canvasWidth , canvasHeight ) ;
179182 this . targetContext . drawImage ( image , x , y , sw , sh ) ;
180183 }
181184
0 commit comments