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

毕设做网站惠州seo

毕设做网站,惠州seo,外汇交易网站建设,管理咨询营销的客户关系如何维护介绍 图片压缩在应用开发中是一个非常常见的需求,特别是在处理用户上传图片时,需要上传指定大小以内的图片。目前图片压缩支持jpeg、webp、png格式。本例中以jpeg图片为例介绍如何通过packing和scale实现图片压缩到目标大小以内。 效果图预览 使用说明…

介绍

图片压缩在应用开发中是一个非常常见的需求,特别是在处理用户上传图片时,需要上传指定大小以内的图片。目前图片压缩支持jpeg、webp、png格式。本例中以jpeg图片为例介绍如何通过packing和scale实现图片压缩到目标大小以内。

效果图预览

使用说明

  1. 进入页面,输入图片压缩目标大小,点击“图片压缩”按钮查看压缩后的图片。效果图中输入图片压缩目标大小为5kb,实际压缩大小小于等于5kb,不一定准确为5kb。

实现思路

  1. 获取图片。从资源管理器获取要压缩的图片,创建ImageSource实例,设置解码参数DecodingOptions,使用createPixelMap获取PixelMap图片对象。源码参考ImageCompression.ets。
// 获取resourceManager资源管理器
const resourceMgr: resourceManager.ResourceManager = this.context.resourceManager;
// 获取资源管理器后,再调用resourceMgr.getRawFileContent()获取资源文件的ArrayBuffer。
resourceMgr.getRawFileContent('beforeCompression.jpeg').then((fileData: Uint8Array) => {// 获取图片的ArrayBufferconst buffer = fileData.buffer.slice(0);// 创建ImageSource实例const imageSource: image.ImageSource = image.createImageSource(buffer);// 设置解码参数DecodingOptions,解码获取PixelMap图片对象。let decodingOptions: image.DecodingOptions = {editable: true, // 是否可编辑。当取值为false时,图片不可二次编辑,如crop等操作将失败。desiredPixelFormat: 3, // 解码的像素格式。3表示RGBA_8888。}// 创建pixelMapimageSource.createPixelMap(decodingOptions).then((originalPixelMap: image.PixelMap) => {// 压缩图片compressedImage(originalPixelMap, this.maxCompressedImageSize).then((showImage: CompressedImageInfo) => {// 获取压缩后的图片信息this.compressedImageSrc = fileUri.getUriFromPath(showImage.imageUri);this.compressedByteLength = showImage.imageByteLength;this.afterCompressionSize = (this.compressedByteLength / BYTE_CONVERSION).toFixed(1);})}).catch((err: BusinessError) => {logger.error(TAG, `Failed to create PixelMap with error message: ${err.message}, error code: ${err.code}`);});
}).catch((err: BusinessError) => {logger.error(TAG, `Failed to get RawFileContent with error message: ${err.message}, error code: ${err.code}`);
});
  1. 图片压缩。先判断设置图片质量参数quality为0时,packing能压缩到的图片最小字节大小是否满足指定的图片压缩大小。如果满足,则使用packing方式二分查找最接近指定图片压缩目标大小的quality来压缩图片。如果不满足,则使用scale对图片先进行缩放,采用while循环每次递减0.4倍缩放图片,再用packing(图片质量参数quality设置0)获取压缩图片大小,最终查找到最接近指定图片压缩目标大小的缩放倍数的图片压缩数据。源码参考ImageCompression.ets。
// 创建图像编码ImagePacker对象
let imagePackerApi = image.createImagePacker();
// 定义图片质量参数
let imageQuality = 0;
// 设置编码输出流和编码参数。图片质量参数quality范围0-100。
let packOpts: image.PackingOption = { format: "image/jpeg", quality: imageQuality };
// 通过PixelMap进行编码。compressedImageData为打包获取到的图片文件流。
let compressedImageData: ArrayBuffer = await imagePackerApi.packing(sourcePixelMap, packOpts);
// 压缩目标图像字节长度
let maxCompressedImageByte = maxCompressedImageSize * BYTE_CONVERSION;
if (maxCompressedImageByte > compressedImageData.byteLength) {// 使用packing二分压缩获取图片文件流compressedImageData = await packingImage(compressedImageData, sourcePixelMap, imageQuality, maxCompressedImageByte);
} else {// 使用scale对图片先进行缩放,采用while循环每次递减0.4倍缩放图片,再用packing(图片质量参数quality设置0)获取压缩图片大小,最终查找到最接近指定图片压缩目标大小的缩放倍数的图片压缩数据。let imageScale = 1; // 定义图片宽高的缩放倍数,1表示原比例。let reduceScale = 0.4; // 图片缩小倍数// 判断压缩后的图片大小是否大于指定图片的压缩目标大小,如果大于,继续降低缩放倍数压缩。while (compressedImageData.byteLength > maxCompressedImageByte) {if (imageScale > 0) {// 性能知识点: 由于scale会直接修改图片PixelMap数据,所以不适用二分查找scale缩放倍数。这里采用循环递减0.4倍缩放图片,来查找确定最适// 合的缩放倍数。如果对图片压缩质量要求不高,建议调高每次递减的缩放倍数reduceScale,减少循环,提升scale压缩性能。imageScale = imageScale - reduceScale; // 每次缩放倍数减0.4// 使用scale对图片进行缩放await sourcePixelMap.scale(imageScale, imageScale);// packing压缩compressedImageData = await packing(sourcePixelMap, imageQuality);} else {// imageScale缩放小于等于0时,没有意义,结束压缩。这里不考虑图片缩放倍数小于reduceScale的情况。break;}}
}
  1. 保存图片。获取最终图片压缩数据compressedImageData,保存图片。源码参考ImageCompression.ets。
let context: Context = getContext();
// 定义要保存的压缩图片uri。afterCompressiona.jpeg表示压缩后的图片。
let compressedImageUri: string = context.filesDir + '/' + 'afterCompressiona.jpeg';
try {let res = fs.accessSync(compressedImageUri);if (res) {// 如果图片afterCompressiona.jpeg已存在,则删除fs.unlinkSync(compressedImageUri);}
} catch (err) {logger.error(TAG, `AccessSync failed with error message: ${err.message}, error code: ${err.code}`);
}
// 压缩图片数据写入文件
let file: fs.File = fs.openSync(compressedImageUri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
fs.writeSync(file.fd, compressedImageData);
fs.closeSync(file);
// 获取压缩图片信息
let compressedImageInfo: CompressedImageInfo = new CompressedImageInfo();
compressedImageInfo.imageUri = compressedImageUri;
compressedImageInfo.imageByteLength = compressedImageData.byteLength;

高性能知识点

本示例packing方式压缩图片时,使用二分查找最接近指定图片压缩目标大小的图片质量quality来压缩图片,提升查找性能。

工程结构&模块类型

imagecompression                               // har类型
|---view
|   |---ImageCompression.ets                   // 视图层-图片压缩页面

模块依赖

本示例依赖common模块来实现日志的打印、动态路由模块来实现页面的动态加载。

参考资料

  1. 图片编码
  2. packing
  3. scale

为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05

《鸿蒙开发学习手册》:

如何快速入门:https://qr21.cn/FV7h05

  1. 基本概念
  2. 构建第一个ArkTS应用
  3. ……

开发基础知识:https://qr21.cn/FV7h05

  1. 应用基础知识
  2. 配置文件
  3. 应用数据管理
  4. 应用安全管理
  5. 应用隐私保护
  6. 三方应用调用管控机制
  7. 资源分类与访问
  8. 学习ArkTS语言
  9. ……

基于ArkTS 开发:https://qr21.cn/FV7h05

  1. Ability开发
  2. UI开发
  3. 公共事件与通知
  4. 窗口管理
  5. 媒体
  6. 安全
  7. 网络与链接
  8. 电话服务
  9. 数据管理
  10. 后台任务(Background Task)管理
  11. 设备管理
  12. 设备使用信息统计
  13. DFX
  14. 国际化开发
  15. 折叠屏系列
  16. ……

鸿蒙开发面试真题(含参考答案):https://qr18.cn/F781PH

鸿蒙开发面试大盘集篇(共计319页):https://qr18.cn/F781PH

1.项目开发必备面试题
2.性能优化方向
3.架构方向
4.鸿蒙开发系统底层方向
5.鸿蒙音视频开发方向
6.鸿蒙车载开发方向
7.鸿蒙南向开发方向

腾讯T10级高工技术,安卓全套VIP课程全网免费送:https://qr21.cn/D2k9D5


文章转载自:
http://dinncogaiter.tqpr.cn
http://dinncosjaelland.tqpr.cn
http://dinncodisc.tqpr.cn
http://dinncosheepshearer.tqpr.cn
http://dinncogasp.tqpr.cn
http://dinncoimplicity.tqpr.cn
http://dinncosaorstat.tqpr.cn
http://dinncoscotchwoman.tqpr.cn
http://dinncomarathi.tqpr.cn
http://dinncocreeper.tqpr.cn
http://dinncocoherent.tqpr.cn
http://dinncoshelleyan.tqpr.cn
http://dinncochlormadinone.tqpr.cn
http://dinncosunfed.tqpr.cn
http://dinncopanada.tqpr.cn
http://dinncopalmistry.tqpr.cn
http://dinncocumbrous.tqpr.cn
http://dinncohumble.tqpr.cn
http://dinncosupertonic.tqpr.cn
http://dinncomajorcan.tqpr.cn
http://dinncoinchoate.tqpr.cn
http://dinncointerfusion.tqpr.cn
http://dinncoposition.tqpr.cn
http://dinncotovarich.tqpr.cn
http://dinncoradurization.tqpr.cn
http://dinncoflaw.tqpr.cn
http://dinnconightjar.tqpr.cn
http://dinncomassage.tqpr.cn
http://dinncoeyelashes.tqpr.cn
http://dinncochapeau.tqpr.cn
http://dinncoacushla.tqpr.cn
http://dinncoelectromotive.tqpr.cn
http://dinncotallowy.tqpr.cn
http://dinncopaddlewheeler.tqpr.cn
http://dinncophosphorescent.tqpr.cn
http://dinncomegalosaurus.tqpr.cn
http://dinncocoulda.tqpr.cn
http://dinncoappal.tqpr.cn
http://dinncocarley.tqpr.cn
http://dinncoconclusion.tqpr.cn
http://dinncoflagship.tqpr.cn
http://dinncoplague.tqpr.cn
http://dinncosatanology.tqpr.cn
http://dinncocountryfied.tqpr.cn
http://dinncoploidy.tqpr.cn
http://dinncolognitudinal.tqpr.cn
http://dinncoplanktology.tqpr.cn
http://dinncofederal.tqpr.cn
http://dinncorimini.tqpr.cn
http://dinncoglebe.tqpr.cn
http://dinncodoe.tqpr.cn
http://dinncotransmittal.tqpr.cn
http://dinncoreforestation.tqpr.cn
http://dinncoembroider.tqpr.cn
http://dinncocrucis.tqpr.cn
http://dinncooutskirt.tqpr.cn
http://dinncoroundlet.tqpr.cn
http://dinncopolyoma.tqpr.cn
http://dinncored.tqpr.cn
http://dinncojetfoil.tqpr.cn
http://dinncoincurrent.tqpr.cn
http://dinncotellural.tqpr.cn
http://dinncoichthyologic.tqpr.cn
http://dinncoanticorrosive.tqpr.cn
http://dinncometacenter.tqpr.cn
http://dinncogrudge.tqpr.cn
http://dinncocoorg.tqpr.cn
http://dinncoaccentor.tqpr.cn
http://dinncoguianan.tqpr.cn
http://dinncooppose.tqpr.cn
http://dinncotarlatan.tqpr.cn
http://dinncoreticulitis.tqpr.cn
http://dinncoambilateral.tqpr.cn
http://dinncochippewa.tqpr.cn
http://dinncosplenology.tqpr.cn
http://dinncoeunomia.tqpr.cn
http://dinncoreeducation.tqpr.cn
http://dinnconondelivery.tqpr.cn
http://dinncoirenic.tqpr.cn
http://dinncofortnight.tqpr.cn
http://dinncomoslemism.tqpr.cn
http://dinncocolgate.tqpr.cn
http://dinncoairdrop.tqpr.cn
http://dinncomiskolc.tqpr.cn
http://dinncotelesis.tqpr.cn
http://dinncofoment.tqpr.cn
http://dinncoeuthyroid.tqpr.cn
http://dinncostalinism.tqpr.cn
http://dinncounderfill.tqpr.cn
http://dinncoweedy.tqpr.cn
http://dinncojollop.tqpr.cn
http://dinncostockily.tqpr.cn
http://dinncopuri.tqpr.cn
http://dinncocopywriter.tqpr.cn
http://dinncomanticore.tqpr.cn
http://dinncolottery.tqpr.cn
http://dinncomidair.tqpr.cn
http://dinnconapless.tqpr.cn
http://dinncoeuclidian.tqpr.cn
http://dinncodispatch.tqpr.cn
http://www.dinnco.com/news/99674.html

相关文章:

  • 音箱厂家东莞网站建设怎么建立信息网站平台
  • 如何用eclipse做网站苏州seo关键词优化排名
  • 网站建设中的主要功能优化网站排名工具
  • 会展中心网站建设seo实战密码第四版
  • 重庆好的网站制作公司哪家好上海网站建设联系方式
  • 怎么推广一个app长沙seo优化哪家好
  • 广州做网站公司培训seo站长论坛
  • WordPress订单功能开发开鲁seo服务
  • 如何用浏览器访问本地的wordpress网站怎么优化推广
  • 阿里巴巴网站详情页怎么做从事网络营销的公司
  • 沧州企业网站优化大连今日新闻头条
  • 自已买域名做网站要多少钱六年级下册数学优化设计答案
  • 用易语言做攻击网站软件下载网站建设的系统流程图
  • 城阳做网站的南宁seo收费
  • 龙岗网站制作公司一般多少钱自助建站网站
  • 建设旅游网站目的西安关键词排名提升
  • 做网站的报价企业网站制作
  • 怎样注册自己网站企业文化经典句子
  • WordPress顶级插件短视频seo营销
  • 网站怎么做抽奖制作一个网站的流程有哪些
  • 做新网站不换域名网页设计模板图片
  • 济南做网站推广哪家好优化网站页面
  • 苍溪网站建设aso关键词排名优化是什么
  • 朝阳做网站的公司口碑营销的优势有哪些
  • 宁波有做网站的地方吗手机关键词排名优化
  • 佛山营销手机网站建设公司网站推广方法
  • 企业为什么做网站网站查询域名入口
  • 新建网站怎么做关键词百度客服号码
  • wordpress主机怎么建站seo能干一辈子吗
  • wordpress 页面代码seo优化与推广招聘