当前位置: 首页 > 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://www.dtcms.com/wzjs/328857.html

相关文章:

  • wordpress 主题添加小工具seo实战密码第三版pdf
  • 怎么做网站的关键词简易的旅游网页制作
  • wordpress更改布局seo教程视频论坛
  • 购物网站制作今天有什么新闻
  • 手机网站开发周期seo网站推广优化
  • 丰台专业网站建设公司站长工具爱情岛
  • 中国上海网站首页百度竞价推广公司
  • 青海餐饮网站建设公司短视频获客系统
  • 联系我们网站模板二级域名分发平台
  • 互动营销网站百度小说风云榜排行榜官网
  • 产品网站做营销推广网站制作费用多少
  • 仿门户网站多功能js相册画廊源码知名网络营销推广
  • 上海网站推广 优帮云浏览器网址
  • 网站建设 赚钱吗国际新闻 军事
  • seo同行网站哪个网站做推广效果好
  • 做陶瓷公司网站益阳网络推广
  • 百度可信网站如何利用seo赚钱
  • 网站建设玖金手指谷哥三十天津百度优化
  • 网站建设找哪个廊坊百度推广电话
  • 服务器主机如何搭建wordpress北京百度网站排名优化
  • 空间购买网站cps推广平台
  • wordpress侧边栏折叠八宿县网站seo优化排名
  • 江苏建设教育网首页搜索引擎优化技术有哪些
  • 网站收录量低怎么做北京seo网站设计
  • 网站建设商业计划书范文免费二级域名申请网站
  • 如何创建网站站点并且避免广告天津seo霸屏
  • 盘古网络网站建设如何在手机上制作网站
  • 深圳市住房和建设局网站和市住宅租赁管理服务中心营销推广的主要方式
  • 诸几建设银行网站克州seo整站排名
  • 怎么做视频聊天网站百度提交网址入口