Pre Merge pull request !139 from Megasu/N/A

This commit is contained in:
Megasu 2024-05-07 03:48:57 +00:00 committed by Gitee
commit f927fa4cad
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
1 changed files with 57 additions and 35 deletions

View File

@ -12,38 +12,40 @@
* 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 {ImageKnife} from '@ohos/libraryimageknife' import { ImageKnife, ImageKnifeGlobal, OnCompressListener, OnRenameListener } from '@ohos/libraryimageknife';
import {OnRenameListener} from '@ohos/libraryimageknife'
import {OnCompressListener} from '@ohos/libraryimageknife'
import {ImageKnifeGlobal} from '@ohos/libraryimageknife'
@Entry @Entry
@Component @Component
struct CompressPage { struct CompressPage {
@State mRPixelMap?: PixelMap = undefined; @State mRPixelMap?: PixelMap = undefined;
@State mFPixelMap?: PixelMap = undefined; @State mFPixelMap?: PixelMap = undefined;
@State mResultText: string= "压缩回调结果" @State mResultText: string = "压缩回调结果"
@State mResultPath: string= "压缩路径:" @State mResultPath: string = "压缩路径:"
@State mAsyncPath: string= "" @State mAsyncPath: string = ""
@State mAsyncPathHint: string= "" @State mAsyncPathHint: string = ""
build() { build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) {
Column() { Column() {
Column() { Column() {
Text("Resource image compress").fontColor(Color.Gray).fontSize(16); Text("Resource image compress")
.fontColor(Color.Gray)
.fontSize(16);
Button() { Button() {
Text("同步压缩").fontSize(13).fontColor(Color.White) Text("同步压缩")
.fontSize(13)
.fontColor(Color.White)
} }
.height(35) .height(35)
.width(120) .width(120)
.margin({ top: 10 }) .margin({ top: 10 })
.onClick(() => { .onClick(() => {
this.compressAsyncRecource(); this.compressAsyncResource();
}); });
Scroll(){ Scroll() {
Text(this.mAsyncPathHint).fontSize(13) Text(this.mAsyncPathHint)
.fontSize(13)
} }
.height(55) .height(55)
.width(350) .width(350)
@ -56,46 +58,64 @@ struct CompressPage {
.backgroundColor(Color.Pink) .backgroundColor(Color.Pink)
Button() { Button() {
Text($r("app.string.resource_image_compress")).fontSize(13).fontColor(Color.White) Text($r("app.string.resource_image_compress"))
.fontSize(13)
.fontColor(Color.White)
} }
.height(35) .height(35)
.width(120) .width(120)
.margin({ top: 10 }) .margin({ top: 10 })
.onClick(() => { .onClick(() => {
this.cropressRecource(); this.compressResource();
}); });
Image(this.mRPixelMap == undefined?'': this.mRPixelMap!) Image(this.mRPixelMap == undefined ? '' : this.mRPixelMap!)
.width(200) .width(200)
.height(200) .height(200)
.margin({ top: 10 }) .margin({ top: 10 })
Text(this.mResultText).fontColor(Color.Gray).fontSize(16); Text(this.mResultText)
Text(this.mResultPath).fontColor(Color.Gray).fontSize(16); .fontColor(Color.Gray)
}.margin({ top: 10 }); .fontSize(16);
Text(this.mResultPath)
.fontColor(Color.Gray)
.fontSize(16);
}
.margin({ top: 10 });
Column() { Column() {
Text("file image compress").fontColor(Color.Gray).fontSize(16); Text("file image compress")
.fontColor(Color.Gray)
.fontSize(16);
Button() { Button() {
Text($r("app.string.file_image_compress")).fontSize(13).fontColor(Color.White) Text($r("app.string.file_image_compress"))
.fontSize(13)
.fontColor(Color.White)
} }
.height(35) .height(35)
.width(120) .width(120)
.margin({ top: 10 }) .margin({ top: 10 })
.onClick(() => { .onClick(() => {
}); });
Image(this.mFPixelMap == undefined ?'':this.mFPixelMap!) Image(this.mFPixelMap == undefined ? '' : this.mFPixelMap!)
.width(200) .width(200)
.height(200) .height(200)
.margin({ top: 10 }); .margin({ top: 10 });
}.margin({ top: 10 }).visibility(Visibility.Hidden); }
}.margin({ bottom: 30 }); .margin({ top: 10 })
}.width('100%').height('100%'); .visibility(Visibility.Hidden);
}
.margin({ bottom: 30 });
}
.width('100%')
.height('100%');
} }
private compressAsyncRecource() {
private compressAsyncResource() {
let data = new Array<Resource>(); let data = new Array<Resource>();
data.push($r('app.media.jpgSample')) data.push($r('app.media.jpgSample'))
let imageKnife:ImageKnife|undefined = ImageKnifeGlobal.getInstance().getImageKnife() let imageKnife: ImageKnife | undefined = ImageKnifeGlobal.getInstance()
if(imageKnife!=undefined) { .getImageKnife()
if (imageKnife != undefined) {
imageKnife imageKnife
.compressBuilder() .compressBuilder()
.load(data) .load(data)
@ -108,7 +128,8 @@ struct CompressPage {
} }
console.info("asasd start compress end") console.info("asasd start compress end")
} }
private cropressRecource() {
private compressResource() {
let data = new Array<Resource>(); let data = new Array<Resource>();
data.push($r('app.media.jpgSample')) data.push($r('app.media.jpgSample'))
let rename: OnRenameListener = { let rename: OnRenameListener = {
@ -118,12 +139,12 @@ struct CompressPage {
} }
let listener: OnCompressListener = { let listener: OnCompressListener = {
start:()=>{ start: () => {
this.mResultText = "start" this.mResultText = "start"
console.info("asasd start") console.info("asasd start")
}, },
onSuccess:(p: PixelMap | null | undefined, path: string)=> { onSuccess: (p: PixelMap | null | undefined, path: string) => {
if(p!=null && p!=undefined) { if (p != null && p != undefined) {
let pack = p; let pack = p;
this.mRPixelMap = pack as PixelMap; this.mRPixelMap = pack as PixelMap;
console.info("asasd success path:" + this.mRPixelMap) console.info("asasd success path:" + this.mRPixelMap)
@ -131,14 +152,15 @@ struct CompressPage {
this.mResultPath = path; this.mResultPath = path;
} }
}, },
onError:(s: string)=>{ onError: (s: string) => {
console.info("asasd onError:" + s) console.info("asasd onError:" + s)
this.mResultText = "fail"; this.mResultText = "fail";
} }
} }
console.info("asasd start compress") console.info("asasd start compress")
let imageKnife:ImageKnife|undefined = ImageKnifeGlobal.getInstance().getImageKnife() let imageKnife: ImageKnife | undefined = ImageKnifeGlobal.getInstance()
if(imageKnife != undefined) { .getImageKnife()
if (imageKnife != undefined) {
imageKnife imageKnife
.compressBuilder() .compressBuilder()
.load(data) .load(data)