
harmony-utils 一款功能丰富且极易上手的HarmonyOS工具库,借助众多实用工具类,致力于助力开发者迅速构建鸿蒙应用。其封装的工具涵盖了APP、设备、屏幕、授权、通知、线程间通信、弹框、吐司、生物认证、用户首选项、拍照、相册、扫码、文件、日志,异常捕获、字符、字符串、数字、集合、日期、随机、base64、加密、解密、JSON等一系列的功能和操作,能够满足各种不同的开发需求。 picker_utils 是harmony-utils拆分出来的一个子库,包含PickerUtil、PhotoHelper、ScanUtil。
下载安装
ohpm i @pura/harmony-utils
ohpm i @pura/picker_utils
//全局初始化方法,在UIAbility的onCreate方法中初始化 AppUtil.init()
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
AppUtil.init(this.context);
}let pixelMap1 = await SnapshotUtil.get('snapshot_id1');
let pixelMap2 = SnapshotUtil.getSync('snapshot_id2');SnapshotUtil.createFromBuilder(() => {
this.RandomBuilder()
}).then((pixelMap) => {
Utils.showSheetImg(pixelMap);
}).catch((err: BusinessError) => {
ToastUtil.showToast("组件生成图片异常!");
});
@Builder
RandomBuilder() {
Column() {
Text("Builder截图")
.fontSize(20)
.fontWeight(FontWeight.Bold)
.fontStyle(FontStyle.Italic)
.margin({ top: 20 })
Image($r("app.media.test_as4"))
.margin({ top: 30, bottom: 10 })
.padding(10)
.width('95%')
.backgroundColor(Color.Pink)
}
.backgroundColor(Color.Blue)
.padding(10)
.width('100%')
.aspectRatio(1)
.margin({ top: 50 })
.id("rBuilder")
}let pixelMap3 = await SnapshotUtil.snapshot();SnapshotUtil.onSnapshotListener(() => {
ToastUtil.showToast("系统截图了!");
LogUtil.error("系统截图了!");
}); SnapshotUtil.removeSnapshotListener();
SnapshotUtil.removeSnapshotListener(snapshotCallBack);import { router } from '@kit.ArkUI';
import { LogUtil, SnapshotUtil, ToastUtil } from '@pura/harmony-utils';
import { DescribeBean } from '../../model/DescribeBean';
import { BusinessError } from '@kit.BasicServicesKit';
import { MockSetup } from '@ohos/hamock';
import { TitleBarView } from '../../component/TitleBarView';
import { Utils } from '../../utils/Utils';
/**
* 组件截图和窗口截图工具类
*/
@Entry
@Component
struct Index {
private scroller: Scroller = new Scroller();
@State describe: DescribeBean = router.getParams() as DescribeBean;
private snapshotCallBack: VoidCallback = () => { //系统截图监听回调
ToastUtil.showToast("您已成功截图!");
LogUtil.error("您已成功截图!");
}
@MockSetup
mock() {
this.describe = new DescribeBean("SnapshotUtil", "组件截图和窗口截图工具类");
}
build() {
Column() {
TitleBarView({ describe: this.describe })
Divider()
Scroll(this.scroller) {
Column() {
Button("get()")
.btnStyle()
.onClick(async () => {
let pixelMap1 = await SnapshotUtil.get('snapshot_id1');
Utils.showSheetImg(pixelMap1);
})
Button("getSync()")
.btnStyle()
.onClick(() => {
let pixelMap1 = SnapshotUtil.getSync('snapshot_id2');
Utils.showSheetImg(pixelMap1);
})
Button("createFromBuilder()")
.btnStyle()
.onClick(() => {
SnapshotUtil.createFromBuilder(() => {
this.RandomBuilder()
}).then((pixelMap) => {
Utils.showSheetImg(pixelMap);
}).catch((err: BusinessError) => {
LogUtil.error("createFromBuilder-异常信息:\n" + JSON.stringify(err));
ToastUtil.showToast("组件生成图片异常!");
});
})
Button("snapshot()")
.btnStyle()
.onClick(async () => {
let pixelMap3 = await SnapshotUtil.snapshot();
Utils.showSheetImg(pixelMap3);
})
Button("onSnapshotListener()")
.btnStyle()
.onClick(() => {
SnapshotUtil.onSnapshotListener(() => {
ToastUtil.showToast("系统截图了!");
LogUtil.error("系统截图了!");
});
})
Button("removeSnapshotListener()")
.btnStyle()
.onClick(() => {
SnapshotUtil.removeSnapshotListener();
})
Button("onSnapshotListener()-指定")
.btnStyle()
.onClick(() => {
SnapshotUtil.onSnapshotListener(this.snapshotCallBack);
})
Button("removeSnapshotListener()-指定")
.btnStyle()
.onClick(() => {
SnapshotUtil.removeSnapshotListener(this.snapshotCallBack);
})
Column() {
Text("id组件截图1")
.fontSize(16)
.fontWeight(FontWeight.Bold)
.fontStyle(FontStyle.Italic)
.margin({ top: 10 })
Column() {
Text("id组件截图2")
.fontSize(12)
.fontWeight(FontWeight.Bold)
.fontStyle(FontStyle.Italic)
.margin({ top: 10 })
Image($r("app.media.test_as3"))
.margin({ top: 10, bottom: 10 })
.borderRadius(6)
}
.backgroundColor(Color.Pink)
.borderRadius(10)
.padding(10)
.margin({ top: 10 })
.width('95%')
.id('snapshot_id2')
}
.backgroundColor(Color.Brown)
.border({
width: 5,
radius: 5,
color: Color.Orange,
style: BorderStyle.Dashed
})
.padding(10)
.width('90%')
.margin({ top: 50 })
.id('snapshot_id1')
Blank().layoutWeight(1)
}
.margin({ top: 5, bottom: 5 })
}
.layoutWeight(1)
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Start)
.backgroundColor($r('app.color.main_background'))
}
@Builder
RandomBuilder() {
Column() {
Text("Builder截图")
.fontSize(20)
.fontWeight(FontWeight.Bold)
.fontStyle(FontStyle.Italic)
.margin({ top: 20 })
Image($r("app.media.test_as4"))
.margin({ top: 30, bottom: 10 })
.padding(10)
.width('95%')
.backgroundColor(Color.Pink)
}
.backgroundColor(Color.Blue)
.padding(10)
.width('100%')
.aspectRatio(1)
.margin({ top: 50 })
.id("rBuilder")
}
}
@Styles
function btnStyle() {
.width('90%')
.margin({ top: 10, bottom: 5 })
}原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。