当前位置: 首页 > wzjs >正文

流放之路做装备词缀网站浙江建设厅

流放之路做装备词缀网站,浙江建设厅,中国万网域名注册免费,建设银行流水查询网站*本文共四个功能函数,相当于四个插件。作者为了偷懒写成了一个插件,调对应的函数即可。 1、chooseImageHarmony函数:拉起相册选择图片并转为Base64 2、takePhotoAndConvertToBase64函数:拉起相机拍照并转为Base64 3、openBrows…

*本文共四个功能函数,相当于四个插件。作者为了偷懒写成了一个插件,调对应的函数即可。

1、chooseImageHarmony函数:拉起相册选择图片并转为Base64
2、takePhotoAndConvertToBase64函数:拉起相机拍照并转为Base64
3、openBrowser函数:打开外部的华为浏览器并跳转
4、openAppMarket函数:打开鸿蒙应用商店并跳转

以下为实现的核心代码

index.uts
import picker from '@ohos.file.picker';
import fs from '@ohos.file.fs';
import image from '@ohos.multimedia.image';
import { ImageUtil } from '@pura/harmony-utils';import picker2 from '@ohos.multimedia.cameraPicker'
import camera from '@ohos.multimedia.camera';
import common from '@ohos.app.ability.common';
// import { BusinessError } from '@ohos.base';
import fileuri from '@ohos.file.fileuri';
// import fs from '@ohos.file.fs';
import { buffer } from '@kit.ArkTS';
import { image as image2 } from '@kit.ImageKit';import { Want, common as common2 } from '@kit.AbilityKit';// 拉起相册选择图片并转为Base64
export async function chooseImageHarmony(): Promise<string> {let file: fs.File | null = null;let imageSource: image.ImageSource | null = null;let pixelMap: image.PixelMap | null = null;try {// 1. 调用文件选择器const photoPicker = new picker.PhotoViewPicker();const selectOptions: picker.PhotoSelectOptions = {maxSelectNumber: 1, // 限制只能选择 1 张图片MIMEType: picker.PhotoViewMIMETypes.IMAGE_TYPE, // 可选:限制图片类型};const fileResult = await photoPicker.select(selectOptions);// 2. 获取到URIconst uri = fileResult.photoUris[0];// 3. 使用文件描述符file = fs.openSync(uri, fs.OpenMode.READ_ONLY);// 4. 创建ImageSource并获取PixelMapimageSource = image.createImageSource(file.fd);// 5. 获取图片属性来得到原始尺寸const imageInfo = await imageSource.getImageInfo();const originalWidth = imageInfo.size.width;const originalHeight = imageInfo.size.height;// 6. 定义压缩选项 - 使用正确的类型声明const decodingOptions: image.DecodingOptions = {desiredSize: {width: Math.min(800, originalWidth),  // 不超过800pxheight: Math.round((Math.min(800, originalWidth) / originalWidth) * originalHeight)},desiredPixelFormat: image.PixelMapFormat.RGBA_8888,// 以下是必须包含的默认值desiredRegion: {size: { width: originalWidth, height: originalHeight },x: 0,y: 0},editable: false};pixelMap = await imageSource.createPixelMap(decodingOptions);// 7. 转换为Base64const base64Str: string = await ImageUtil.pixelMapToBase64Str(pixelMap);return base64Str} catch (error) {return ""} finally {imageSource?.release()pixelMap?.release();// 关闭文件(使用 fs.close(file.fd))if (file?.fd !== undefined) {fs.close(file.fd); // ✅ HarmonyOS NEXT 使用 fs.close(fd)}}
}class CameraPosition {cameraPosition: camera.CameraPosition;saveUri: string;constructor(cameraPosition: camera.CameraPosition, saveUri: string) {this.cameraPosition = cameraPosition;this.saveUri = saveUri;}
}// 拉起相机拍照并转为Base64
export async function takePhotoAndConvertToBase64(): Promise<string> {let base64Str = ''// 获取上下文const context = getContext() as common.Context;const pathDir = context.filesDir;// 文件临时路径const filePath = pathDir + '/' + new Date().getTime() + '.jpg';fs.createRandomAccessFileSync(filePath, fs.OpenMode.CREATE);const uri = fileuri.getUriFromPath(filePath);try {// 唤起相机let pickerProfile = new CameraPosition(camera.CameraPosition.CAMERA_POSITION_BACK, uri);await picker2.pick(context, [picker2.PickerMediaType.PHOTO], pickerProfile);const file = await fs.open(uri, fs.OpenMode.READ_ONLY);const imageSource: image.ImageSource = image2.createImageSource(file.fd);const imagePackerApi = image2.createImagePacker();const packOpts: image.PackingOption = { format: "image/jpeg", quality: 70 };const imageBuffer = await imagePackerApi.packing(imageSource, packOpts);base64Str = buffer.from(imageBuffer).toString('base64');const base64Str2 = 'data:image/jpeg;base64,' + base64Str;return base64Str2;} catch (error) {// let err = error as BusinessError;console.error(`拍照失败`);return '';} finally {}
}// 打开外部的华为浏览器并跳转
export function openBrowser(url: string): void {// 1. 获取正确的上下文const context = getContext() as common2.UIAbilityContext;// 2. 明确定义 Want 类型const want: Want = {"action": "ohos.want.action.viewData","entities": ["entity.system.browsable"],"uri": url,"type": "text/plain"};// 3. 正确处理 Promise 类型context.startAbility(want).then((): void => {  // 明确指定返回类型console.log('openUrl: successfully');}).catch((err: Error): void => {  // 明确指定返回类型和错误类型console.error('openURL Failed:', err);});
}// 打开鸿蒙应用商店并跳转
export function openAppMarket(url: string): void {// 1. 获取正确的上下文const context = getContext() as common2.UIAbilityContext;const appMarketWant: Want = {uri: url,};context.startAbility(appMarketWant).then((): void => {console.log('AppMarket opened successfully');}).catch((err: Error): void => {console.error('Failed to open AppMarket:', err);});
}

使用示例:

导入插件:
import { chooseImageHarmony, takePhotoAndConvertToBase64, openBrowser, openAppMarket } from “@/uni_modules/ywx-getImageToBase64”;

调用函数:
1、const base64Data = await chooseImageHarmony();
2、const base64Data = await takePhotoAndConvertToBase64();
3、openBrowser(“https://hmapp.gisgm.cn/tdjg”)
4、openAppMarket(“store://appgallery.huawei.com/app/detail?id=” + ‘com.example.shilrey’)

作者已经实现的功能,有任何疑问请在评论区讨论。


文章转载自:

http://IU51k87n.ggLrt.cn
http://iDrqWDDg.ggLrt.cn
http://STcQKY5B.ggLrt.cn
http://9YmMogaE.ggLrt.cn
http://78AE9TpK.ggLrt.cn
http://u6iHP8uv.ggLrt.cn
http://yUDb9uaq.ggLrt.cn
http://S8Rq09r8.ggLrt.cn
http://Rbmb8o79.ggLrt.cn
http://FvdelrFz.ggLrt.cn
http://vzrsX1On.ggLrt.cn
http://lSfibLmj.ggLrt.cn
http://gjAEE4fO.ggLrt.cn
http://AxIQTyXj.ggLrt.cn
http://1fbA39Vr.ggLrt.cn
http://jF3mEJVs.ggLrt.cn
http://mpcAqOzB.ggLrt.cn
http://EIA6bkur.ggLrt.cn
http://Fz8D0ChV.ggLrt.cn
http://j82a5OuB.ggLrt.cn
http://Rn67xyDC.ggLrt.cn
http://yY2Hgt50.ggLrt.cn
http://6HVBX7mv.ggLrt.cn
http://tBIAexgi.ggLrt.cn
http://uvoSwAc0.ggLrt.cn
http://VVVKc4aJ.ggLrt.cn
http://80gxeTBs.ggLrt.cn
http://x6fUnrgz.ggLrt.cn
http://BQsCJeaB.ggLrt.cn
http://HPeIQvSQ.ggLrt.cn
http://www.dtcms.com/wzjs/636021.html

相关文章:

  • 南宁市西乡塘区建设局网站网页设计如何报价
  • 重庆报考网安卓优化大师下载
  • 浙江网站建设商城价格女生电子商务专业适合做什么
  • 网站如何上传到主机优化营商环境心得体会2023
  • 设计前沿的网站市场策划是做什么的
  • 建设报名系统网站网络公司网站官网
  • 大型定制网站最贵建设多少钱政务网站建设管理工作总结
  • 宁波做网站优化中山手机网站建设
  • 安陆做网站多少钱遵义水网站建设
  • 上海浦东新区网站深圳市创同盟科技有限公司
  • 网站使用的主色调沧州网站建设公司电话
  • 网站建设图片怎么动网创
  • 网站开发项目计划书ppt专业的广州微网站建设
  • 百度网站官网入口19
  • 如何选择企业网站建设重庆荣昌网站建设报价
  • 网络设计网站用什么来网站开发好
  • 旅游网站建设规划济南做seo外包
  • 移动端网站怎么做的网站换域名影响
  • 蛋糕教做网站wordpress 插件放在那
  • 昆明网站建设推荐瑞安外贸网站建设
  • 网站改版开发公司怎么做网站教程 用的工具
  • 东莞高端商城网站制作珠海商城网站制作
  • 网站开发交接热狗seo优化外包
  • 建设信用交通网站 省网站的建设服务平台
  • 可以做免费推广的网站吗创新驱动发展战略纲要
  • 专业建站提供商高薪聘请网站开发工程师
  • 有网站源码怎么做网站电子商务网站建设 教学大纲
  • 做网站那几步建设网站的价值
  • 做网站要多少像素微博图片怎么做外链到网站
  • 网站引流推广怎么做动态链接做网站外链图