技术标签: java Java基础 thumbnails 图片操作
最近工作上用到图片旋转,所以来偏图片旋转的文章。其中碰到不少坑。
以下实现了旋转90度的倍数。方便我们了解旋转是怎么做的 ,如果要旋转任意角度,可以用Java自带的thumbnails(文章后面介绍)
import org.springframework.util.Base64Utils;
import net.coobird.thumbnailator.Thumbnails;
/**
* 旋转图片
* @param old_img
* @param degess 旋转度数 负数为逆时针 //如果宽高比大于1 不旋转
* @return
* @throws IOException
*/
public static BufferedImage rotateImage(BufferedImage bi, String times ) {
int width = bi.getWidth();
int height = bi.getHeight();
BufferedImage biFlip = null;
try {
int count = Integer.valueOf(times);
if(count>=4){
count = count % 4;
}
if(count==0) return bi;
if(count==1){
biFlip = new BufferedImage(height,width, bi.getType());
for(int i=0; i<width; i++){
for(int j=0; j<height; j++){
biFlip.setRGB(height-1-j, i, bi.getRGB(i, j));//顺时针旋转
//旋转公式 R*(x,y) = (-y,x)
}
}
}else if(count==2){
//旋转180度
biFlip = new BufferedImage(width,height, bi.getType());
for(int i=0; i<width; i++){
for(int j=0; j<height; j++){
biFlip.setRGB(width-1-i, height-1-j, bi.getRGB(i, j));//顺时针旋转180
}
}
}else {
//逆时针旋转90 == 旋转270度
biFlip = new BufferedImage(height,width, bi.getType());
for(int i=0; i<width; i++){
for(int j=0; j<height; j++){
biFlip.setRGB(j, width-1-i, bi.getRGB(i, j));
}
}
}
} catch(Exception e){
e.printStackTrace();
return bi;
}finally {
}
return biFlip;
}
图片转base64
public static String encodeToString(BufferedImage image) {
String imageString = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ImageIO.write(image, "jpg", bos);
byte[] imageBytes = bos.toByteArray();
imageString = Base64Utils.encodeToString(imageBytes);
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return imageString;
}
base64转图片
public static BufferedImage baseToBufferedImage(String base64Sign){
ByteArrayOutputStream os = new ByteArrayOutputStream();
BufferedImage img = null;
byte[] buffer=null;
BufferedImage bufferImg =null ;
try {
buffer = Base64Utils.decodeFromString(base64Sign);
ByteArrayInputStream input = new ByteArrayInputStream(buffer);
bufferImg = ImageIO.read(input);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return bufferImg;
}
**
**
1 Thumbnails.of(images)
<!-- https://mvnrepository.com/artifact/net.coobird/thumbnailator -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
可以是BufferedImage File InputStream URL 等途径
**
**
a 压缩图片(拉伸图片)
压缩图片(选择操作的大小) size()是按照原图一width为准等比例压缩。forceSize()是强制压缩,可能导致图片变形。
也可以用scale指定比例,第二个方法可以指定长款分别压缩多少倍,等于图片拉伸操作。
b 旋转图片
Thumbnails.of(“”).scale(1).rotate(10);
旋转度数按顺时针旋转,如果要逆时针,可以传入负数。必须先指定大小既scale或用size指定。
3**
**
可以作为输出的方式也挺多的 。
总结 :
不管哪种方式实现,还是要注意几个问题
1 是图片旋转后编码的时候用BASE64Decoder编码解码,而BASE64Decoder编码时默认每隔76个字符加一个换行,导致旋转后编码变的很大,并且有换行的编码在转图片的时候就有问题,用spring自带的工具org.springframework.util.Base64Utils编解码;。
2是对于空白图片,在转成BufferedImage时Java把透明背景设置成了黑色背景,可以在操作之前把图片设置成白色背景。
图片剪裁
/**
* 图片中心400*400的区域
*/
Thumbnails.of("images/test.jpg").sourceRegion(Positions.CENTER, 400, 400).size(200, 200).keepAspectRatio(false)
.toFile("C:/image_region_center.jpg");
/**
* 图片右下400*400的区域
*/
Thumbnails.of("images/test.jpg").sourceRegion(Positions.BOTTOM_RIGHT, 400, 400).size(200, 200).keepAspectRatio(false)
.toFile("C:/image_region_bootom_right.jpg");
/**
* 指定坐标
*/
Thumbnails.of("images/test.jpg").sourceRegion(600, 500, 400, 400).size(200, 200).keepAspectRatio(false).toFile("C:/image_region_coord.jpg");
/**
* watermark(位置,水印图,透明度)
*/
Thumbnails.of("images/test.jpg").size(1280, 1024).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("images/watermark.png")), 0.5f)
.outputQuality(0.8f).toFile("C:/image_watermark_bottom_right.jpg");
Thumbnails.of("images/test.jpg").size(1280, 1024).watermark(Positions.CENTER, ImageIO.read(new File("images/watermark.png")), 0.5f)
.outputQuality(0.8f).toFile("C:/image_watermark_center.jpg");
文章浏览阅读3.6k次。用人工智能普惠体育发展。
文章浏览阅读10w+次,点赞92次,收藏495次。表单提交方式一:直接利用form表单提交html页面代码:<!DOCTYPE html><html><head><meta charset="UTF-8" /><title>Insert title here</title></head><body><form action="h..._提交表单
文章浏览阅读5.1k次。Unity Spine SkeletonGraphic 重复播放 过度残影Bug 解决方案不推荐使用SetToSetupPose和Setup Pose相关,代码直接贴上/// <summary>/// Spine播放设置/// </summary>/// <param name="trackIndex">填写0</param>/// <param name="animationName">动画名</param>/// &l_unity skeletongraphic
文章浏览阅读3.5k次。一、推导过程:二、结果:边缘分布x1,x2 各自依然服从 μi,写反差矩阵 Σii 的多元高斯分布;条件概率分布给定 xj 求 xi 的分布:μi|j=μi+ΣijΣ−1jj(xj−μj)Σi|j=Σjj−ΣTijΣ−1iiΣij..._高斯分布的条件概率
文章浏览阅读339次,点赞8次,收藏8次。Ratelimitcache: Python缓存库,支持速率限制项目链接: https://gitcode.com/simonw/ratelimitcache?utm_source=artical_gitcode如果你正在寻找一个Python缓存库,并且希望对缓存操作进行速率限制,那么Ratelimitcache可能是你的理想选择。什么是Ratelimitcache?Ratelimitca..._python ratelimit基于什么
文章浏览阅读2.3k次,点赞2次,收藏8次。Xpath和CSS信息提取的方法异同点_xpath 获取css
文章浏览阅读454次。正交频分复用(OFDM)是一种在现代通信系统中广泛使用的调制技术,它具有高效的频谱利用和抗多径衰落等特点。64QAM(64-ary Quadrature Amplitude Modulation)是一种调制方式,可以在每个符号中传输更多的位信息。在OFDM系统中,保持载波同步对确保数据传输的可靠性至关重要。_基于ofdm+64qam系统的载波同步matlab仿真,
文章浏览阅读67次。Jdk1.8 + Tomcat8.5 + Mysql + HBuilderX(Webstorm也行)+ Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)。若包含,则为maven项目,否则为非maven项目。Springboot毕设项目超市商品销售管理系统37x2w(java+VUE+Mybatis+Maven+Mysql)Springboot + mybatis + Maven + Vue 等等组成,B/S模式 + Maven管理等等。其他版本理论上也可以。_vue+springboot+mybatis商品管理系统
文章浏览阅读3w次,点赞2次,收藏4次。转载自: http://blog.csdn.net/zouqin369/article/details/6913692 今天去公司设置好IP后,无论怎么样都上不了internet,再次打开本地后发现默认网关自动消失,cmd下输入ipconfig后的现象如下: 物理地址. . . . . . . . . . . . . : 00-22-64-55-76-8F DHCP 已启用_禁止修改网关命令
文章浏览阅读482次。Extjs的窗口是可以加载自己的HTML的,但这样两个页面就相当独立了,传参是个问题 ,网上也没有很好的解答清楚,猫猫今天就说清楚这个模式的传参要点。_extjs中打开网页怎么传参
文章浏览阅读811次。win_win port 进程
文章浏览阅读1.2k次。Ch3点到点数据链路层知识点1. 点到点数据链路层要解决的主要问题2. 常见的帧管理(帧定界)方法3. CRC的计算4. 流量控制的基本原理5. 常见错误及其处理机制6. 滑动窗口的概念、形式及工作原理7. ARQ(Automatic Repeat reQuest)协议工作原理:8. 连续ARQ(Go-back-N ARQ)工作原理(特别注意累计确认):9. 选择重传ARQ工作原理10. 了解(高..._hdlc go-back-n