forked from floraachy/ImageKnife
107 lines
3.9 KiB
Plaintext
107 lines
3.9 KiB
Plaintext
/*
|
|
* Copyright (C) 2021 Huawei Device Co., Ltd.
|
|
* Licensed under the Apache License, Version 2.0 (the 'License');
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an 'AS IS' BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
import { CropImage } from '@ohos/libraryimageknife'
|
|
import { CropOptions } from '@ohos/libraryimageknife'
|
|
import { Crop } from '@ohos/libraryimageknife'
|
|
import { RecourseProvider } from '@ohos/libraryimageknife'
|
|
import { PixelMapCrop,Options } from '@ohos/libraryimageknife'
|
|
import { CropCallback } from '@ohos/libraryimageknife'
|
|
import { FileUtils } from '@ohos/libraryimageknife'
|
|
import { ImageKnifeGlobal } from '@ohos/libraryimageknife'
|
|
import { BusinessError } from '@ohos.base'
|
|
import resourceManager from '@ohos.resourceManager';
|
|
import common from '@ohos.app.ability.common'
|
|
|
|
@Entry
|
|
@Component
|
|
export struct CropImagePage2 {
|
|
@State options1: Options = new Options();
|
|
@State cropTap: boolean = false;
|
|
|
|
@State width1: number = 0;
|
|
@State height1: number = 0;
|
|
@State _rotate: number = 0;
|
|
@State _scale: number = 1;
|
|
private _resource: Resource = $r('app.media.bmpSample');
|
|
private settings: RenderingContextSettings = new RenderingContextSettings(true)
|
|
private canvasContext: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
|
|
build() {
|
|
Scroll() {
|
|
Column() {
|
|
Button('点击解析图片')
|
|
.onClick(() => {
|
|
((ImageKnifeGlobal.getInstance().getHapContext() as common.UIAbilityContext).resourceManager as resourceManager.ResourceManager)
|
|
.getMediaContent($r('app.media.bmpSample').id)
|
|
.then((data:Uint8Array) => {
|
|
let arrayBuffer = FileUtils.getInstance().uint8ArrayToBuffer(data);
|
|
let optionx = new Options();
|
|
optionx.setWidth(800)
|
|
.setHeight(800)
|
|
.setCropFunction((err:BusinessError|string, pixelmap:PixelMap|null, sx:number, sy:number) => {
|
|
console.log('PMC setCropFunction callback')
|
|
if (err) {
|
|
console.error('PMC crop err =' + err)
|
|
} else {
|
|
this.width1 = sx * px2vp(1);
|
|
this.height1 = sy * px2vp(1);
|
|
if(pixelmap != null) {
|
|
this.canvasContext.drawImage(pixelmap, 0, 0, this.width1, this.height1)
|
|
}
|
|
}
|
|
})
|
|
optionx.loadBuffer(arrayBuffer, () => {
|
|
this.options1 = optionx;
|
|
})
|
|
})
|
|
|
|
|
|
})
|
|
|
|
PixelMapCrop({ options: $options1, cropTap: this.cropTap })
|
|
|
|
Button('点击裁剪图片')
|
|
.onClick(() => {
|
|
this.cropTap = !this.cropTap;
|
|
})
|
|
|
|
|
|
Canvas(this.canvasContext)
|
|
.width(this.width1)
|
|
.height(this.height1)
|
|
.rotate({
|
|
z: 1,
|
|
centerX: this.width1 / 2,
|
|
centerY: this.height1 / 2,
|
|
angle: this._rotate
|
|
})
|
|
.scale({ x: this._scale, y: this._scale, z: 1.0 })
|
|
.gesture(GestureGroup(GestureMode.Parallel,
|
|
RotationGesture({ fingers: 2 }).onActionUpdate((event?: GestureEvent) => {
|
|
if(event != undefined) {
|
|
this._rotate = event.angle;
|
|
}
|
|
}), PinchGesture({ fingers: 2 }).onActionUpdate((event?: GestureEvent) => {
|
|
if(event != undefined) {
|
|
this._scale = event.scale;
|
|
}
|
|
})))
|
|
}
|
|
.backgroundColor(Color.Brown)
|
|
.width('100%')
|
|
}
|
|
|
|
}
|
|
} |