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

成都科技网站建设电话咨询怎样推广公司的网站

成都科技网站建设电话咨询,怎样推广公司的网站,进口国际博览会上海,上海网站备案在哪里目录 前言一、使用Base64编码方式1、基本方法2、压缩本地图片保存到本地3、压缩网络图片到图片服务器 二、使用thumbnailator工具方式1、导入依赖2、压缩本地图片保存到本地 前言 下面介绍了两种获取图片缩略图的方式,全都不是一次性压缩,如果没有达到设…

目录

  • 前言
  • 一、使用Base64编码方式
    • 1、基本方法
    • 2、压缩本地图片保存到本地
    • 3、压缩网络图片到图片服务器
  • 二、使用thumbnailator工具方式
    • 1、导入依赖
    • 2、压缩本地图片保存到本地

前言

下面介绍了两种获取图片缩略图的方式,全都不是一次性压缩,如果没有达到设置的压缩范围,图片会循环压缩,大家根据自己的需求,可以自己调节最大缩略图的大小范围。

经过测试第二种方式效率更高,但是对于压缩率只是初步设置一个大致范围,还需要更精细化的调试

一、使用Base64编码方式

此方式全都需要使用到基本方法,基本方法如下,自行复制

1、基本方法

/*** 按照宽高比例压缩** @param srcImgData 待压缩图片输入流* @param scale      压缩刻度* @return* @throws IOException* @author CY* @date 2020年11月18日*/private static byte[] compress(byte[] srcImgData, double scale) throws IOException {try {BufferedImage bi = ImageIO.read(new ByteArrayInputStream(srcImgData));int width = (int) (bi.getWidth() * scale); // 源图宽度int height = (int) (bi.getHeight() * scale); // 源图高度Image image = bi.getScaledInstance(width, height, Image.SCALE_SMOOTH);BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics g = tag.getGraphics();g.setColor(Color.RED);g.drawImage(image, 0, 0, null); // 绘制处理后的图g.dispose();ByteArrayOutputStream bOut = new ByteArrayOutputStream();ImageIO.write(tag, "JPEG", bOut);return bOut.toByteArray();} catch (Exception e) {e.printStackTrace();}return null;}

2、压缩本地图片保存到本地

/*** 压缩本地图片保存到本地** @param path          保存文件的路径,精确到最后一个文件夹* @param localPhotoUrl 本地图片路径,精确到文件* @param fileName      新的文件名* @return*/public static String localImageCompress(String path, String localPhotoUrl, String fileName) {try {// 读取文件并转换成字节数组Path filePath = Paths.get(localPhotoUrl);byte[] picByte = Files.readAllBytes(filePath);// 对图片进行压缩byte[] finalCompressPic = compress(picByte, 0.9);BigDecimal filesize = new BigDecimal(finalCompressPic.length);BigDecimal kilobyte = new BigDecimal(1024);float returnValue = filesize.divide(kilobyte, 2, BigDecimal.ROUND_UP).floatValue();// 输出压缩后的图片大小System.out.println("图片的字节数:" + finalCompressPic.length + "图片的大小:" + returnValue + "KB");// 如果图片大小超过149KB,则继续压缩,直到图片大小小于或等于149KBif (returnValue > 150) {for (int i = 0; i < 9; i++) {double scale = (i + 1) * 0.1;scale = 0.9 - scale;finalCompressPic = compress(finalCompressPic, scale);filesize = new BigDecimal(finalCompressPic.length);kilobyte = new BigDecimal(1024);returnValue = filesize.divide(kilobyte, 2, BigDecimal.ROUND_UP).floatValue();System.out.println("继续压缩图片的字节数:" + finalCompressPic.length + "图片的大小:" + returnValue + "KB");if (returnValue > 150) {continue;} else {break;}}}// 对字节数组Base64编码Base64.Encoder encoder = Base64.getEncoder();String png_base64 = encoder.encodeToString(finalCompressPic);// 将Base64编码后的字符串转换成字节数组并保存到文件中byte[] decodedBytes = Base64.getDecoder().decode(png_base64);File outputFile = new File(path, fileName);OutputStream outputStream = new FileOutputStream(outputFile);outputStream.write(decodedBytes);// 返回生成的缩略图路径return outputFile.getAbsolutePath();} catch (IOException e) {e.printStackTrace();}return null;}

3、压缩网络图片到图片服务器

//压缩网络图片保存到图片服务器public static String netImageCompress(String photoUrl) {try {byte[] picByte = ImgBase64Util.imgToByte(photoUrl);byte[] compressPic = compress(picByte, 0.9);byte[] finalCompressPic = compressPic;BigDecimal filesize = new BigDecimal(compressPic.length);BigDecimal kilobyte = new BigDecimal(1024);float returnValue = filesize.divide(kilobyte, 2, BigDecimal.ROUND_UP).floatValue();//输出System.out.println("图片的字节数:" + compressPic.length + "图片的大小:" + returnValue + "KB");if (returnValue > 149) {for (int i = 0; i < 10; i++) {double scale = (i + 1) * 0.1;scale = 0.9 - scale;finalCompressPic = compress(finalCompressPic, scale);BigDecimal filesize2 = new BigDecimal(finalCompressPic.length);BigDecimal kilobyte2 = new BigDecimal(1024);float returnValue2 = filesize2.divide(kilobyte2, 2, BigDecimal.ROUND_UP).floatValue();System.out.println("循环i=" + i + "的图片的字节数:" + finalCompressPic.length + "图片的大小:" + returnValue2 + "KB");if (returnValue2 > 149) {continue;} else {break;}}}Base64.Encoder encoder = Base64.getEncoder();String png_base64 = encoder.encodeToString(finalCompressPic);File file = ImgBase64Util.base64ToFile(png_base64);FileInputStream input = new FileInputStream(file);MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain", input);Map param = new HashMap();param.put("source", "system");param.put("type", "img");HttpUtils httpUtils = new HttpUtils();HttpResult httpResult = httpUtils.doFilePost("https://test.com/upload", multipartFile, param);Integer httpCode = httpResult.getCode();if (httpCode == 200) {JSONObject result = JSONObject.parseObject(httpResult.getBody());Integer code = result.getInteger("code");if (code == 0) {String url = result.getString("data");System.out.println("compress pic url:" + url);return url;}}return null;} catch (Exception e) {e.printStackTrace();return null;}}

二、使用thumbnailator工具方式

此方式需要导入依赖,依赖如下

1、导入依赖

	<dependency><groupId>net.coobird</groupId><artifactId>thumbnailator</artifactId><version>0.4.19</version></dependency>

2、压缩本地图片保存到本地

/*** 压缩本地图片保存到本地** @param path          保存文件的路径,精确到最后一个文件夹* @param localPhotoUrl 本地图片路径,精确到文件* @param fileName      新的文件名* @return*/public static String localImageCompress(String path, String localPhotoUrl, String fileName) {try {File f = new File(localPhotoUrl);long sourceImgValue = f.length() / 1024;BufferedImage sourceImg = ImageIO.read(new FileInputStream(f));int width = sourceImg.getWidth();//原图宽度int height = sourceImg.getHeight();//原图高度int hightValue = width;//宽度和高度的最大值if (height > width) hightValue = height;double scale111 = 0.5;if (1000 <= hightValue && hightValue < 2000) scale111 = 0.3;if (2000 <= hightValue && hightValue < 3000) scale111 = 0.2;if (3000 <= hightValue && hightValue < 4000) scale111 = 0.1;if (4000 <= hightValue) scale111 = BigDecimal.valueOf(500L).divide(BigDecimal.valueOf(hightValue), 2, BigDecimal.ROUND_UP).doubleValue();System.out.println("最终scale为:" + scale111);/*** 图片小于5M,压缩图片最大尺寸为512K* 大于5M,小于10M,压缩图片最大尺寸为1M* 大于10M,压缩图片最大尺寸为2M*/int maxSize = 512;//压缩图片的最大尺寸if (sourceImgValue < 5 * 1024) maxSize = 512;if (5 * 1024 <= sourceImgValue && sourceImgValue < 10 * 1024) maxSize = 1024;if (10 * 1024 <= sourceImgValue) maxSize = 2 * 1024;String thumbnailPath = path + fileName;Thumbnails.of(localPhotoUrl).scale(scale111).outputQuality(0.9).toFile(thumbnailPath);File f1 = new File(thumbnailPath);long returnValue = f1.length() / 1024;System.out.println("第一次生成图片缩略图,大小为:" + returnValue + "KB");// 如果图片大小超过1M,则继续压缩,直到图片大小小于或等于149KBif (returnValue > maxSize) {for (int i = 0; i < 4; i++) {double scale = (i + 1) * 0.1;double scaleQuality = 0.9 - scale * 2;if (scale111 > scale) scale111 = scale111 - scale;System.out.println("最终scale为:" + scale111);System.out.println("最终scaleQuality为:" + scaleQuality);Thumbnails.of(localPhotoUrl).scale(scale111).outputQuality(scaleQuality).toFile(thumbnailPath);File f2 = new File(thumbnailPath);returnValue = f2.length() / 1024;System.out.println("继续压缩图片大小:" + returnValue + "KB");if (returnValue > maxSize) {continue;} else {break;}}}return thumbnailPath;} catch (IOException e) {e.printStackTrace();}return null;}

文章转载自:
http://ageusia.xsfg.cn
http://dutiful.xsfg.cn
http://selector.xsfg.cn
http://stairhead.xsfg.cn
http://toxemia.xsfg.cn
http://scopa.xsfg.cn
http://queening.xsfg.cn
http://tokodynamometer.xsfg.cn
http://towing.xsfg.cn
http://jerkin.xsfg.cn
http://hallo.xsfg.cn
http://quinquepartite.xsfg.cn
http://delirifacient.xsfg.cn
http://diffusion.xsfg.cn
http://martha.xsfg.cn
http://serological.xsfg.cn
http://clumber.xsfg.cn
http://motivity.xsfg.cn
http://gladness.xsfg.cn
http://tropopause.xsfg.cn
http://bifoliolate.xsfg.cn
http://drivership.xsfg.cn
http://undemanding.xsfg.cn
http://showboat.xsfg.cn
http://unredeemable.xsfg.cn
http://planform.xsfg.cn
http://operatic.xsfg.cn
http://austral.xsfg.cn
http://intertie.xsfg.cn
http://foreverness.xsfg.cn
http://radula.xsfg.cn
http://sclerotic.xsfg.cn
http://bellflower.xsfg.cn
http://sorta.xsfg.cn
http://unceasingly.xsfg.cn
http://featured.xsfg.cn
http://khalifa.xsfg.cn
http://lithophile.xsfg.cn
http://dagan.xsfg.cn
http://imm.xsfg.cn
http://sensory.xsfg.cn
http://owenite.xsfg.cn
http://unharmed.xsfg.cn
http://grama.xsfg.cn
http://acritical.xsfg.cn
http://panmixia.xsfg.cn
http://celebrant.xsfg.cn
http://crossbreed.xsfg.cn
http://vexed.xsfg.cn
http://winthrop.xsfg.cn
http://phrenic.xsfg.cn
http://scenario.xsfg.cn
http://corelation.xsfg.cn
http://olympic.xsfg.cn
http://bystander.xsfg.cn
http://feckly.xsfg.cn
http://james.xsfg.cn
http://hebrewwise.xsfg.cn
http://overtask.xsfg.cn
http://ganov.xsfg.cn
http://homeomorphous.xsfg.cn
http://pachisi.xsfg.cn
http://circumvolute.xsfg.cn
http://elasticized.xsfg.cn
http://folknik.xsfg.cn
http://porcelaneous.xsfg.cn
http://phrenogastric.xsfg.cn
http://ayahuasca.xsfg.cn
http://rightism.xsfg.cn
http://spilth.xsfg.cn
http://barents.xsfg.cn
http://hindu.xsfg.cn
http://quandary.xsfg.cn
http://yellows.xsfg.cn
http://filiation.xsfg.cn
http://counter.xsfg.cn
http://ogo.xsfg.cn
http://recall.xsfg.cn
http://machining.xsfg.cn
http://wicked.xsfg.cn
http://wellhandled.xsfg.cn
http://refreeze.xsfg.cn
http://referee.xsfg.cn
http://chaffcutter.xsfg.cn
http://managerialist.xsfg.cn
http://ig.xsfg.cn
http://udag.xsfg.cn
http://eyeball.xsfg.cn
http://thermoduric.xsfg.cn
http://colorant.xsfg.cn
http://swaggeringly.xsfg.cn
http://illuminate.xsfg.cn
http://kofta.xsfg.cn
http://overheat.xsfg.cn
http://miscode.xsfg.cn
http://bobwhite.xsfg.cn
http://lexan.xsfg.cn
http://psf.xsfg.cn
http://mizzensail.xsfg.cn
http://subnuclear.xsfg.cn
http://www.hrbkazy.com/news/76104.html

相关文章:

  • 国外网站建设公司成都百度推广优化创意
  • 网站建设基础教程视频梅花seo 快速排名软件
  • 环保材料东莞网站建设临沂森拓网络科技有限公司
  • 手机网站网站开发流程高报师培训机构排名
  • 网站如何做分站seo企业培训班
  • sql可以做网站吗网站流量排名查询工具
  • 凡科做的网站为什么搜不到百度24小时人工电话
  • 烟台市住房和城乡建设厅网站石家庄网站建设案例
  • 商务网站建设实训心得友情链接检测
  • 网站商城建设合同seo权威入门教程
  • 英文网站如何做千锋教育培训机构地址
  • 个人主页界面网站宁德市自然资源局
  • 百度站长联盟网站的seo方案
  • 用h5做网站首页代码关键词优化顾问
  • 科讯cms网站管理系统kesioncms百度统计代码安装位置
  • 别人帮我做的网站没用要交费用吗快速排名优化seo
  • 大学生家教网站开发谷歌搜索引擎入口google
  • v电影主题 wordpress武汉seo管理
  • 做IT的会做网站吗网站建设需求模板
  • 佛山做网站格福州短视频seo方法
  • 建站公司兴田德润实惠品牌营销服务
  • 网站开发后端做那些西安seo王
  • 手机彩票网站开发查关键词热度的网站
  • 网络营销可以做什么工作响应式网站 乐云seo品牌
  • 武汉网站关键词注册百度推广账号
  • 多城市网站如何做seo网站设计软件
  • 网络设置的网站淘宝seo排名优化的方法
  • 网站开发流程相关知识网络营销课程实训总结
  • 做网站如何购买服务器seochan是什么意思
  • 做网站的公司 苏迪天津百度百科