java 将5mb以内图片压缩至200kb以下
package com.qizhi.itfin.controller.mmb;
import java.io.File;
import java.io.IOException;
import com.qizhi.itfin.common.util.ThumbnailUtils;
public class test {
public static void main(String[] args) {
//网络路径地址
String url = "http://192.168.18。。。:81/upload/img//18/3/b4/ba/e9/37/c7/f7/b4bae937c7f7edf048af44e619d18ef4.jpg";
url = pictureCompression(url);
System.err.println(url);
}
private static String pictureCompression(String photoUrl) {
String urls = "http://192.168.18.。。。:81/upload/img/";
//物理路径
String path = "X:/img/";
// 替换后的图片url
photoUrl = photoUrl.replace(urls, path);
// 图片小数点之前的数据
String intNumber = photoUrl.substring(0, photoUrl.indexOf("."));
// 图片小数点之后的数据
String lastFileName = photoUrl.substring(photoUrl.lastIndexOf("."));
// 压缩图片新的路径
String existPhoto = intNumber + "-thumbnail" + lastFileName;
// 判断之前是否有生成的压缩图片
File existPhotoFile = new File(existPhoto);
String pcZipUrls = null;
String slash = "\\";
String leftSlash = "/";
String canonicalPath = null;
System.out.println("【压缩图片】物理地址头部信息 " + path);
System.out.println("【压缩图片】网络地址头部信息 " + urls);
System.out.println("【压缩图片】转化前的图片路径 " + photoUrl);
if (existPhotoFile.exists()) {
try {
canonicalPath = existPhotoFile.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
System.out.println("【压缩图片】图片服务器中已经存在exception " + e);
}
pcZipUrls = canonicalPath.replace(slash, leftSlash);
photoUrl = pcZipUrls.replace(path, urls);
System.out.println("【压缩图片】图片服务器中已经存在 " + photoUrl);
} else {
File file = new File(photoUrl);
try {
File makeThumbnail = ThumbnailUtils.makeThumbnail(file, existPhotoFile);
// 转化成String类型的url
canonicalPath = makeThumbnail.getCanonicalPath();
pcZipUrls = canonicalPath.replace(slash, leftSlash);
photoUrl = pcZipUrls.replace(path, urls);
System.out.println("【压缩图片】转化后的图片路径 " + photoUrl);
} catch (IOException e) {
e.printStackTrace();
System.out.println("【压缩图片】转化后的图片路径exception " + e);
}
}
return photoUrl;
}
}
图片压缩Utils类
/**
* 图片压缩缩略图
*
* @param oldFile
* @param newFile
* @return
* @throws IOException
*/
public static File makeThumbnail(File oldFile, String uri) throws IOException {
long fileSize = oldFile.length();
File resultFile = oldFile;
String fileUri = uri.substring(0, uri.lastIndexOf("."));
File newFile = new File(fileUri);
File newExistFile = new File(uri);
// 格式转换成jpg
String lastFileName = oldFile.getName().substring(oldFile.getName().lastIndexOf(".")).toLowerCase();
if (!StringUtils.equals(lastFileName, ".jpg")) {
Thumbnails.of(oldFile).scale(1f).outputFormat("jpg").outputQuality(0.8f).toFile(newFile);
fileSize = newExistFile.length();
resultFile = newExistFile;
}
// 处理文件大小到200K以下
if (fileSize >= MAX_SIZE) {
// 第一次将图片尺寸变小,高度固定480
Thumbnails.of(oldFile).height(480).outputFormat("jpg").outputQuality(0.8f).toFile(newFile);
fileSize = newExistFile.length();
// 一般这时图片就在200K以下了,以防万一,循环压缩质量,直到200KB以下
while (fileSize > MAX_SIZE) {
Thumbnails.of(newExistFile).scale(1D).outputFormat("jpg").outputQuality(0.8f).toFile(newFile);
fileSize = newExistFile.length();
}
resultFile = newExistFile;
}
return resultFile;
}
另外一种图片压缩方式(内存流) 并 转成base64
package com.qizhi.itfin.controller.gps;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import org.apache.tomcat.util.codec.binary.Base64;
public class test {
public static void main(String[] args) {
try {
String convertImageToBase64 = convertImageToBase64(
"http://*********/upload/img//18/3/f4/74/90/ff/af/6f/f47490ffaf6f89d027dfe59cfa9e9fda.jpg",
200);
System.err.println(convertImageToBase64);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 将图片转换为base64格式
*
* @param imageUrl:图片路径
* @param sizeLimit:原图大小上限,当图片原图大小超过该值时先将图片大小 设置为该值以下再转换成base64格式,单位kb
* @return
*/
public static String convertImageToBase64(String imageUrl, Integer sizeLimit) throws IOException {
// 默认上限为500k
if (sizeLimit == null) {
sizeLimit = 500;
}
sizeLimit = sizeLimit * 1024;
String base64Image;
DataInputStream dataInputStream = null;
ByteArrayOutputStream outputStream = null;
ByteArrayInputStream inputStream = null;
try {
// 从远程读取图片
URL url = new URL(imageUrl);
dataInputStream = new DataInputStream(url.openStream());
outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
byte[] context = outputStream.toByteArray();
// 将图片数据还原为图片
inputStream = new ByteArrayInputStream(context);
BufferedImage image = ImageIO.read(inputStream);
int imageSize = context.length;
int type = image.getType();
int height = image.getHeight();
int width = image.getWidth();
BufferedImage tempImage;
// 判断文件大小是否大于size,循环压缩,直到大小小于给定的值
System.out.println(sizeLimit);
while (imageSize > sizeLimit) {
System.err.println(imageSize);
// 将图片长宽压缩到原来的90%
height = new Double(height * 0.9).intValue();
width = new Double(width * 0.9).intValue();
tempImage = new BufferedImage(width, height, type);
// 绘制缩小后的图
tempImage.getGraphics().drawImage(image, 0, 0, width, height, null);
// 重新计算图片大小
outputStream.reset();
ImageIO.write(tempImage, "jpg", outputStream);
imageSize = outputStream.toByteArray().length;
}
// 将图片转化为base64并返回
byte[] data = outputStream.toByteArray();
File destFile = new File("X:\\vedio\\456.jpg");
FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流
out.write(data);
out.close();
// 此处一定要使用org.apache.tomcat.util.codec.binary.Base64,防止再linux上出现换行等特殊符号
base64Image = Base64.encodeBase64String(data);
} catch (Exception e) {
// 抛出异常
throw e;
} finally {
if (dataInputStream != null) {
try {
dataInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return base64Image;
}
}
java 将5mb以内图片压缩至200kb以下相关推荐
- java高清无损图片压缩
Java高清无损图片压缩 (本文禁止转载,如需转载请联系本人:微信/QQ同号:969987665) 简单介绍 thumbnailator-0.4.5.jar 官方下载网址 语法使用(超级简单的,再也没 ...
- Java使用thumbnailator进行图片压缩缩放裁剪水印旋转处理
Java使用thumbnailator进行图片压缩缩放裁剪水印旋转处理 一.thumbnailator介绍 二.使用步骤 1.maven的pom.xml引入如下 2.测试代码 一.thumbnaila ...
- Java之~ 上传 图片压缩,阿里图片压缩,图片旋转方法工具类
需要的jar jai_codec-1.1.3.jar,jai_core-1.1.3.jar,simpleimage-1.2.0.jar,metadata-extractor-2.3.1.jar(旋转时 ...
- Thumbnailator实现图片压缩
今天给大家介绍一个非常好用的一个java开源的Thumbnailator图片压缩jar,随着智能手机的像素越来越高,用户把手机拍摄的3-10兆的图片上传到服务器上,在从服务器上面读取上传的图片会加载非 ...
- linux下进行图片压缩(pdf转换为jpg)
linux下进行图片压缩(pdf转换为jpg) 1.安装imagemagic rpm -vih imagemagic-版本号.rpm --force --nodeps rpm -vih imageMa ...
- java 图片压缩100k_如何将图片压缩到100K以内,教你几种免费方法
一般我们在提交电子证件照时都要求100K以内的图片,超过则无法正常上传,那么有没有免费将图片压缩变小的方法呢?教你3个简单又免费的方法,收藏起来备用吧. 方法1:最简单粗暴的方式就是直接打开图片后用聊 ...
- 解决java 图片压缩图片图片变色问题
最近有人遇到在处理在用Java进行图片压缩的时候,压缩后图片的背景色发生了变色,如图所示: 压缩前: 压缩后: 经查阅相关资料后发现可以用阿里巴巴的SImpleImage可以解决这个问题. 主要依赖以 ...
- 怎么把图片压缩到200K以内?如何在线压缩图片大小?
现在很多网站都对上传的图片有大小限制,比如图片不能超过200K,那么怎么才能将过大的图片压缩到200k以内呢?可是使用图片压缩指定大小工具来压缩图片大小,下面详细介绍一下这款图片压缩(https:// ...
- 如何快速图片压缩指定大小?图片压缩到200k以内的方法
图片压缩到200k以内的介绍 在现代社交媒体和网页设计中,高质量的图片是必不可少的.但是,大型图像文件可能会导致页面加载时间过长,从而影响用户体验.这时就需要使用图片压缩技术来将图片文件大小减小到合理 ...
- 图片压缩到200k以内怎么操作?图片压缩指定大小工具推荐
工作中经常出现图片太大无法发送或者是上传的情况,这时候就需要将图片压缩才能上传,那么怎么把图片压缩到200K以内呢?可以使用图片压缩指定大小(https://www.yasuotu.com/image ...
最新文章
- DL:神经网络算法简介之耗算力的简介、原因、经典模型耗算力计算、GPU使用之详细攻略
- 浅谈Nginx服务器的内部核心架构设计
- rsync 服务器文件同步备份
- hive解绑邮箱_django使用QQ企业邮箱发送邮件
- 计算机学硕和专硕编码,2018考研报名:学会从专业代码判断学硕还是专硕
- 微信圈子将于12月28日停止运营,网友:不是微信朋友圈?
- GCC与VC2013性能比较
- HLI测试 涉及书籍
- python 自相关_自相关与偏自相关的简单介绍
- 用计算机软件绘制思维导图,电脑软件绘制思维导图操作教程分享
- 钟平逻辑英语语法_钟平逻辑英语教程视频1-5季全(含笔记 支持百度云)
- OpenKG开源系列 | 大规模中文概念图谱OpenConcepts (浙江大学)
- 【FPGA】组合逻辑 3-8译码器实现
- 微信删除和拉黑哪个更绝情?
- 拆解大数据总线平台DBus的系统架构
- 组件化之路 - ViewBinding基类封装
- Beyond Short Snippets: Deep Networks for Video Classification
- ME525+ 刷机工具及设置中心号码
- 解决element 表单验证不通过的俩大坑
- 只安装XCode’s Command line tools不安装XCode