!73 解决变换存在的已知问题

Merge pull request !73 from zhoulisheng2/master
This commit is contained in:
openharmony_ci 2023-12-01 08:40:39 +00:00 committed by Gitee
commit 89fcb3a5d3
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 86 additions and 109 deletions

View File

@ -207,35 +207,11 @@ struct IndexFunctionDemo {
}.width('100%').height(60).backgroundColor(Color.Pink)
Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Button("大量照片和重复数据")
.onClick(() => {
console.log("pages/manyPhotoShowPage 页面跳转")
router.pushUrl({ url: "pages/manyPhotoShowPage" });
}).margin({ top: 15 })
Button("图片加载暂停和恢复")
.onClick(() => {
console.log("pages/manyPhotoShowPage 页面跳转")
router.pushUrl({ url: "pages/photosPausedResumedPage" });
}).margin({ top: 15 })
Button("部分url测试")
.onClick(() => {
console.log("pages/tempUrlTestPage 页面跳转")
router.pushUrl({ url: "pages/tempUrlTestPage" });
}).margin({ top: 15 })
Button("测试drawFactory")
.onClick(() => {
console.log("pages/drawFactoryTestPage 页面跳转")
router.pushUrl({ url: "pages/drawFactoryTestPage" });
}).margin({ top: 15 })
}.width('100%').height(60).backgroundColor(Color.Pink) }.width('100%').height(60).backgroundColor(Color.Pink)
Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Button("大量照片和重复数据") Button("大量照片")
.onClick(() => { .onClick(() => {
console.log("pages/manyPhotoShowPage 页面跳转") console.log("pages/manyPhotoShowPage 页面跳转")
router.pushUrl({ url: "pages/manyPhotoShowPage" }); router.pushUrl({ url: "pages/manyPhotoShowPage" });
@ -254,12 +230,12 @@ struct IndexFunctionDemo {
}.width('100%').height(60).backgroundColor(Color.Pink) }.width('100%').height(60).backgroundColor(Color.Pink)
Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Button("图片加载暂停和恢复") Button("图片暂停和恢复")
.onClick(() => { .onClick(() => {
console.log("pages/photosPausedResumedPage 页面跳转") console.log("pages/photosPausedResumedPage 页面跳转")
router.pushUrl({ url: "pages/photosPausedResumedPage" }); router.pushUrl({ url: "pages/photosPausedResumedPage" });
}).margin({ top: 15 }) }).margin({ top: 15 })
Button("List滑动时暂停图片加载,滑动结束恢复图片加载") Button("List滑动暂停和恢复")
.onClick(() => { .onClick(() => {
console.log("pages/photosPausedResumedPage2 页面跳转") console.log("pages/photosPausedResumedPage2 页面跳转")
router.pushUrl({ url: "pages/photosPausedResumedPage2" }); router.pushUrl({ url: "pages/photosPausedResumedPage2" });

View File

@ -12,96 +12,103 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import {AsyncTransform} from '../transform/AsyncTransform' import { AsyncTransform } from '../transform/AsyncTransform'
import image from '@ohos.multimedia.image' import image from '@ohos.multimedia.image'
import {Size} from '../../imageknife/RequestOption' import { Size } from '../../imageknife/RequestOption'
import { BusinessError } from '@ohos.base' import { BusinessError } from '@ohos.base'
import { Constants } from '../constants/Constants'
export class TransformUtils { export class TransformUtils {
static centerCrop(buf: ArrayBuffer, outWidth: number, outHeihgt: number, static centerCrop(buf: ArrayBuffer, outWidth: number, outHeihgt: number,
callback?: AsyncTransform<Promise<PixelMap>>) { callback?: AsyncTransform<Promise<PixelMap>>) {
let imageSource:image.ImageSource = image.createImageSource(buf); let imageSource: image.ImageSource = image.createImageSource(buf);
imageSource.getImageInfo() imageSource.getImageInfo()
.then((p) => { .then((p) => {
let sw:number=0; let sw: number = 0;
let sh:number=0; let sh: number = 0;
let scale:number=1; let scale: number = 1;
let pw:number = p.size.width; let pw: number = p.size.width;
let ph:number = p.size.height; let ph: number = p.size.height;
if (pw == outWidth && ph == outHeihgt) { if (pw == outWidth && ph == outHeihgt) {
sw = outWidth; sw = outWidth;
sh = outHeihgt; sh = outHeihgt;
} else { } else {
if (pw * outHeihgt > outWidth * ph) { if (pw * outHeihgt > outWidth * ph) {
scale = outHeihgt / ph; scale = outHeihgt / ph;
} else { } else {
scale = outWidth / pw; scale = outWidth / pw;
} }
sw = pw * scale; sw = pw * scale;
sh = ph * scale; sh = ph * scale;
} }
let options:image.DecodingOptions = { let options: image.DecodingOptions = {
editable: true, editable: true,
rotate: 0, rotate: 0,
desiredRegion: { size: { width: sw, height: sh }, desiredRegion: { size: { width: sw, height: sh },
x: pw / 2 - sw / 2, x: pw / 2 - sw / 2,
y: ph / 2 - sh / 2, y: ph / 2 - sh / 2,
}, },
} }
if (callback) { if (callback) {
callback.asyncTransform('', imageSource.createPixelMap(options)); callback.asyncTransform('', imageSource.createPixelMap(options));
} }
}) })
.catch((error:BusinessError) => { .catch((error: BusinessError) => {
callback?.asyncTransform(error, null); callback?.asyncTransform(error, null);
}) })
} }
static rotateImage(buf: ArrayBuffer, degreesToRotate: number): Promise<PixelMap>{ static rotateImage(buf: ArrayBuffer, degreesToRotate: number): Promise<PixelMap> {
let imageSource:image.ImageSource = image.createImageSource(buf); let imageSource: image.ImageSource = image.createImageSource(buf);
let options:image.DecodingOptions = { let options: image.DecodingOptions = {
editable: true, editable: true,
rotate: degreesToRotate rotate: degreesToRotate
} }
let promise:Promise<PixelMap> = imageSource.createPixelMap(options); let promise: Promise<PixelMap> = imageSource.createPixelMap(options);
imageSource.release() imageSource.release()
return promise; return promise;
} }
static centerInside(buf: ArrayBuffer, outWidth: number, outHeihgt: number, static centerInside(buf: ArrayBuffer, outWidth: number, outHeihgt: number,
callback?: AsyncTransform<Promise<PixelMap>>) { callback?: AsyncTransform<PixelMap>) {
let imageSource:image.ImageSource = image.createImageSource(buf); let imageSource: image.ImageSource = image.createImageSource(buf);
imageSource.getImageInfo() imageSource.getImageInfo()
.then((p:image.ImageInfo) => { .then((p: image.ImageInfo) => {
let pw = p.size.width; let pw = p.size.width;
let ph = p.size.height; let ph = p.size.height;
if (pw <= outWidth && ph <= outHeihgt) { if (pw <= outWidth && ph <= outHeihgt) {
let promise:Promise<PixelMap> = imageSource.createPixelMap() imageSource.createPixelMap()
imageSource.release() .then(p => {
callback?.asyncTransform('', promise); callback?.asyncTransform('', p);
imageSource.release()
}).catch((e: BusinessError) => {
callback?.asyncTransform(Constants.PROJECT_TAG + ';CenterInside error:' + e, null);
imageSource.release()
})
} else { } else {
TransformUtils.fitCenter(buf, outWidth, outHeihgt, callback); TransformUtils.fitCenter(buf, outWidth, outHeihgt, callback);
} }
}) })
.catch((error:BusinessError) => { .catch((error: BusinessError) => {
callback?.asyncTransform(error, null); callback?.asyncTransform(error, null);
}) })
} }
static fitCenter(buf: ArrayBuffer, outWidth: number, outHeihgt: number static fitCenter(buf: ArrayBuffer, outWidth: number, outHeihgt: number
, callback?: AsyncTransform<Promise<PixelMap>>) { , callback?: AsyncTransform<PixelMap>) {
let imageSource:image.ImageSource = image.createImageSource(buf); let imageSource: image.ImageSource = image.createImageSource(buf);
imageSource.getImageInfo() imageSource.getImageInfo()
.then((p:image.ImageInfo) => { .then((p: image.ImageInfo) => {
let pw:number = p.size.width; let pw: number = p.size.width;
let ph:number = p.size.height; let ph: number = p.size.height;
let widthPercentage:number = outWidth / pw; let widthPercentage: number = outWidth / pw;
let heightPercentage:number = outHeihgt / ph; let heightPercentage: number = outHeihgt / ph;
let minPercentage:number = Math.min(widthPercentage, heightPercentage); let minPercentage: number = Math.min(widthPercentage, heightPercentage);
let targetWidth:number = Math.round(minPercentage * pw); let targetWidth: number = Math.round(minPercentage * pw);
let targetHeight:number = Math.round(minPercentage * ph); let targetHeight: number = Math.round(minPercentage * ph);
if (pw == targetWidth && ph == targetHeight) { if (pw == targetWidth && ph == targetHeight) {
targetWidth = pw; targetWidth = pw;
@ -110,18 +117,24 @@ export class TransformUtils {
targetWidth = minPercentage * pw; targetWidth = minPercentage * pw;
targetHeight = minPercentage * ph; targetHeight = minPercentage * ph;
} }
let options:image.DecodingOptions = { let options: image.DecodingOptions = {
editable: true, editable: true,
rotate: 0, rotate: 0,
desiredSize: { width: targetWidth, height: targetHeight } desiredSize: { width: targetWidth, height: targetHeight }
} }
if (callback) { if (callback != undefined) {
let promise:Promise<PixelMap> = imageSource.createPixelMap(options); imageSource.createPixelMap(options)
imageSource.release(); .then(p => {
callback.asyncTransform('', promise); callback?.asyncTransform('', p);
imageSource.release();
})
.catch((e: BusinessError) => {
callback?.asyncTransform(Constants.PROJECT_TAG + ';FitCenter error:' + e, null);
imageSource.release();
})
} }
}) })
.catch((e:BusinessError) => { .catch((e: BusinessError) => {
if (callback) { if (callback) {
callback.asyncTransform(e, null); callback.asyncTransform(e, null);
} }
@ -132,13 +145,13 @@ export class TransformUtils {
if (!imageSource) { if (!imageSource) {
return; return;
} }
imageSource.getImageInfo((err:BusinessError, value:image.ImageInfo) => { imageSource.getImageInfo((err: BusinessError, value: image.ImageInfo) => {
if (err) { if (err) {
func?.asyncTransform(err, null) func?.asyncTransform(err, null)
return; return;
} }
let pWidth:number = value.size.width; let pWidth: number = value.size.width;
let pHeight:number = value.size.height; let pHeight: number = value.size.height;
func?.asyncTransform('', { width: pWidth, height: pHeight }); func?.asyncTransform('', { width: pWidth, height: pHeight });
}) })
} }

View File

@ -13,7 +13,7 @@
* limitations under the License. * limitations under the License.
*/ */
import {BaseTransform} from '../BaseTransform' import {BaseTransform} from '../BaseTransform'
import {AsyncTransform} from '../AsyncTransform' import {AsyncTransform} from '../AsyncTransform'
import {Constants} from '../../constants/Constants' import {Constants} from '../../constants/Constants'
import {TransformUtils} from '../TransformUtils' import {TransformUtils} from '../TransformUtils'
@ -31,12 +31,6 @@ export class CenterInside implements BaseTransform<PixelMap> {
} }
return; return;
} }
TransformUtils.centerInside(buf, request.size.width, request.size.height, {asyncTransform:(error:BusinessError|string, data:Promise<PixelMap>|null) => { TransformUtils.centerInside(buf, request.size.width, request.size.height, func)
data?.then(p => {
func?.asyncTransform('', p);
}).catch((e:BusinessError) => {
func?.asyncTransform(Constants.PROJECT_TAG + ';CenterInside error:' + e, null);
})
}})
} }
} }

View File

@ -31,12 +31,6 @@ export class FitCenter implements BaseTransform<PixelMap> {
} }
return; return;
} }
TransformUtils.fitCenter(buf, request.size.width, request.size.height, {asyncTransform:(error:BusinessError|string, data:Promise<PixelMap>|null) => { TransformUtils.fitCenter(buf, request.size.width, request.size.height, func)
data?.then(p => {
func?.asyncTransform('', p);
}).catch((e:BusinessError) => {
func?.asyncTransform(Constants.PROJECT_TAG + ';FitCenter error:' + e, null);
})
}})
} }
} }