1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 根据地址获得数据的字节流
*
* @param strUrl
* 网络连接地址
* @return
*/
public
static
byte
`[] getImageFromNetByUrl(String strUrl) {`
try
{
URL url =
new
URL(strUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(
`"GET"`);
conn.setConnectTimeout(
`5
*
1000`);
InputStream inStream = conn.getInputStream();
`// 通过输入流获取图片数据`
byte
`[] btImg = readInputStream(inStream);`// 得到图片的二进制数据
return
btImg;
}
catch
(Exception e) {
e.printStackTrace();
}
return
null
`;`
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* 根据地址获得数据的字节流
*
* @param strUrl
* 本地连接地址
* @return
*/
public
static
byte
`[] getImageFromLocalByUrl(String strUrl) {`
try
{
File imageFile =
new
File(strUrl);
InputStream inStream =
new
FileInputStream(imageFile);
byte
`[] btImg = readInputStream(inStream);`// 得到图片的二进制数据
return
btImg;
}
catch
(Exception e) {
e.printStackTrace();
}
return
null
`;`
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* 从输入流中获取数据
*
* @param inStream
* 输入流
* @return
* @throws Exception
*/
public
static
byte
`[] readInputStream(InputStream inStream)
throws
Exception {`
ByteArrayOutputStream outStream =
new
ByteArrayOutputStream();
byte
`[] buffer =
new
byte[
10240`];
int
len =
0
`;`
while
((len = inStream.read(buffer)) != -
`1`) {
outStream.write(buffer,
0
`, len);`
}
inStream.close();
return
outStream.toByteArray();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
byte
`[] btImg1 = ImageUtil.getImageFromNetByUrl(fileUrl1);`
if
(
`null
!= btImg1 && btImg1.length >
0`) {
logger.debug(
`"读取到:"
+ btImg1.length +
" 字节"`);
ImageUtil.writeImageToDisk(btImg1, fileZipUrl1);
}
else
{
logger.debug(
`"没有从该连接获得内容"`);
}
byte
`[] btImg2 = ImageUtil.getImageFromNetByUrl(fileUrl2);`
if
(
`null
!= btImg2 && btImg2.length >
0`) {
logger.debug(
`"读取到:"
+ btImg2.length +
" 字节"`);
ImageUtil.writeImageToDisk(btImg2, fileZipUrl2);
}
else
{
logger.debug(
`"没有从该连接获得内容"`);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 将图片写入到磁盘
*
* @param img
* 图片数据流
* @param fileName
* 文件保存时的名称
*/
public
static
void
writeImageToDisk(
`byte`[] img, String zipImageUrl) {
try
{
File file =
new
File(zipImageUrl);
FileOutputStream fops =
new
FileOutputStream(file);
fops.write(img);
fops.flush();
fops.close();
System.out.println(
`"图片已经写入"`+zipImageUrl);
}
catch
(Exception e) {
e.printStackTrace();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import
java.io.*;
import
java.util.Date;
import
java.awt.*;
import
java.awt.image.*;
import
javax.imageio.ImageIO;
import
com.sun.image.codec.jpeg.*;
/**
* 图片压缩处理
*/
public
class
ImgCompress {
private
Image img;
private
int
width;
private
int
height;
/**
* 构造函数
*/
public
ImgCompress(String fileName)
throws
IOException {
File file =
new
File(fileName);
`// 读入文件 `
img = ImageIO.read(file);
// 构造Image对象
width = img.getWidth(
`null`);
// 得到源图宽
height = img.getHeight(
`null`);
// 得到源图长
}
/**
* 按照宽度还是高度进行压缩
* @param w int 最大宽度
* @param h int 最大高度
*/
public
void
resizeFix(
`int
w,
int
h)
throws
IOException { `
if
(width / height > w / h) {
resizeByWidth(w);
}
else
{
resizeByHeight(h);
}
}
/**
* 以宽度为基准,等比例放缩图片
* @param w int 新宽度
*/
public
void
resizeByWidth(
`int
w)
throws
IOException { `
int
h = (
`int`) (height * w / width);
resize(w, h);
}
/**
* 以高度为基准,等比例缩放图片
* @param h int 新高度
*/
public
void
resizeByHeight(
`int
h)
throws
IOException { `
int
w = (
`int`) (width * h / height);
resize(w, h);
}
/**
* 强制压缩/放大图片到固定的大小
* @param w int 新宽度
* @param h int 新高度
*/
public
void
resize(
`int
w,
int
h)
throws
IOException { `
// SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
BufferedImage image =
new
BufferedImage(w, h,BufferedImage.TYPE_INT_RGB );
image.getGraphics().drawImage(img,
0
`,
0, w, h,` `null
);
// 绘制缩小后的图 `
File destFile =
new
File(
`"C:/Users/Administrator/Desktop/147.jpg"`);
FileOutputStream out =
new
FileOutputStream(destFile);
// 输出到文件流
// 可以正常实现bmp、png、gif转jpg
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
// JPEG编码
out.close();
}
1
2
3
4
5
6
7
@SuppressWarnings
`("deprecation"
) `
public
static
void
main(String[] args)
throws
Exception {
System.out.println(
`"开始:"
+
new
Date().toLocaleString()); `
ImgCompress imgCom =
new
ImgCompress(
`"C:/Users/Administrator/Desktop/1479209533362.jpg"`);
imgCom.resizeFix(
`285,` `380
); `
System.out.println(
`"结束:"
+
new
Date().toLocaleString()); `
}
1
}
原网址: 访问
创建于: 2022-12-05 16:37:10
目录: default
标签: 无
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
java windows火焰图_mob64ca12ec8020的技术博客_51CTO博客 - 在windows下不可行,不知道作者是怎样搞的 监听SpringBoot 服务启动成功事件并打印信息_监听springboot启动完毕-CSDN博客 SpringBoot中就绪探针和存活探针_management.endpoint.health.probes.enabled-CSDN博客 u2u转换板 - 嘉立创EDA开源硬件平台 Spring Boot 项目的轻量级 HTTP 客户端 retrofit 框架,快来试试它!_Java精选-CSDN博客 手把手教你打造一套最牛的知识笔记管理系统! - 知乎 - 想法有重合-理论可参考 安宇雨 闲鱼 机械键盘 客制化 开贴记录 文本 linux 使用find命令查找包含某字符串的文件_beijihukk的博客-CSDN博客_find 查找字符串 ---- mac 也适用 安宇雨 打字音 记录集合 B站 bilibili 自行搭建 开坑 真正的客制化 安宇雨 黑苹果开坑 查找工具包maven pom 引用地 工具网站 Dantelis 介绍的玩轴入坑攻略 --- 关于轴的一些说法 --- 非官方 ---- 心得而已 --- 长期开坑更新 [本人问题][新开坑位]关于自动化测试的工具与平台应用 机械键盘 开团 网站记录 -- 能做一个收集的程序就好了 不过现在没时间 -- 信息大多是在群里发的 - 你要让垃圾佬 都去一个地方看难度也是很大的 精神支柱 [超级前台]sprinbboot maven superdesk-app 记录 [信息有用] [环境准备] [基本完成] [sebp/elk] 给已创建的Docker容器增加新的端口映射 - qq_30599553的博客 - CSDN博客 [正在研究] Elasticsearch, Logstash, Kibana (ELK) Docker image documentation elasticsearch centos 安装记录 及 启动手记 正式服务器 39 elasticsearch 问题合集 不断更新 6.1.1 | 6.5.1 两个版本 博客程序 - 测试 - bug记录 等等问题 laravel的启动过程解析 - lpfuture - 博客园 OAuth2 Server PHP 用 Laravel 搭建带 OAuth2 验证的 RESTful 服务 | Laravel China 社区 - 高品质的 Laravel 和 PHP 开发者社区 利用Laravel 搭建oauth2 API接口 附 Unauthenticated 解决办法 - 煮茶的博客 - SegmentFault 思否 使用 OAuth2-Server-php 搭建 OAuth2 Server - 午时的海 - 博客园 基于PHP构建OAuth 2.0 服务端 认证平台 - Endv - 博客园 Laravel 的 Artisan 命令行工具 Laravel 的文件系统和云存储功能集成 浅谈Chromium中的设计模式--终--Observer模式 浅谈Chromium中的设计模式--二--pre/post和Delegate模式 浅谈Chromium中的设计模式--一--Chromium中模块分层和进程模型 DeepMind 4 Hacking Yourself README.md update 20211011
Laravel China 简书 知乎 博客园 CSDN博客 开源中国 Go Further Ryan是菜鸟 | LNMP技术栈笔记 云栖社区-阿里云 Netflix技术博客 Techie Delight Linkedin技术博客 Dropbox技术博客 Facebook技术博客 淘宝中间件团队 美团技术博客 360技术博客 古巷博客 - 一个专注于分享的不正常博客 软件测试知识传播 - 测试窝 有赞技术团队 阮一峰 语雀 静觅丨崔庆才的个人博客 软件测试从业者综合能力提升 - isTester IBM Java 开发 使用开放 Java 生态系统开发现代应用程序 pengdai 一个强大的博主 HTML5资源教程 | 分享HTML5开发资源和开发教程 蘑菇博客 - 专注于技术分享的博客平台 个人博客-leapMie 流星007 CSDN博客 - 舍其小伙伴 稀土掘金 Go 技术论坛 | Golang / Go 语言中国知识社区
最新评论