java.awt.Graphics2D 图片缩放
关键字:java image thumbnail google
粗略demo:
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException; import javax.imageio.ImageIO; import org.junit.Test; public class ImageUtils { @Test
public void test1() throws IOException {
File file = new File("E:\\1.png"); ImageUtils.scale(file, 0.1, new File("E:\\2.jpg"));
ImageUtils.thumbnail(file, 100, new File("E:\\3.jpg"));
} /**
* 按比例缩放
*
* @param rawImage
* @param ratio
* @param saltImage
*
* @throws IOException
* */
public static void scale(File rawImage, double ratio, File saltImage) throws IOException {
BufferedImage rawBufferedImage = ImageIO.read(rawImage);
int width = (int) (rawBufferedImage.getWidth() * ratio);
int height = (int) (rawBufferedImage.getHeight() * ratio);
BufferedImage bi = getCompatibleImage(width, height);
Graphics2D g2d = bi.createGraphics();
double xScale = (double) width / rawBufferedImage.getWidth();
double yScale = (double) height / rawBufferedImage.getHeight();
AffineTransform at = AffineTransform.getScaleInstance(xScale, yScale);
g2d.drawRenderedImage(rawBufferedImage, at);
g2d.dispose();
ImageIO.write(bi, "jpg", saltImage);
} /**
* 按比例缩放至【最大边 = 指定pix】
*
* @param rawImage
* @param pixel
* @param saltImage
*
* @throws IOException
* */
public static void thumbnail(File rawImage, int pixel, File saltImage) throws IOException {
BufferedImage source = ImageIO.read(rawImage);
int width = source.getWidth();
int height = source.getHeight(); int l = width > height ? width : height; double ratio = (pixel * 1.0) / l; int w = (int) (width * ratio);
int h = (int) (height * ratio); BufferedImage bi = getCompatibleImage(w, h);
Graphics2D g2d = bi.createGraphics();
double xScale = (double) w / width;
double yScale = (double) h / height;
AffineTransform at = AffineTransform.getScaleInstance(xScale, yScale);
g2d.drawRenderedImage(source, at);
g2d.dispose();
ImageIO.write(bi, "jpg", saltImage);
} private static BufferedImage getCompatibleImage(int w, int h) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
BufferedImage image = gc.createCompatibleImage(w, h);
return image;
} }
java.awt.Graphics2D 图片缩放的更多相关文章
- Java基础 awt Graphics2D 生成矩形图片并向内写入字符串
JDK :OpenJDK-11 OS :CentOS 7.6.1810 IDE :Eclipse 2019‑03 typesetting :Markdown code ...
- Java基础 awt Graphics2D 生成矩形图片并向其中画一条直线
JDK :OpenJDK-11 OS :CentOS 7.6.1810 IDE :Eclipse 2019‑03 typesetting :Markdown code ...
- java图片缩放
package com.rubekid.springmvc.utils; import java.awt.AlphaComposite; import java.awt.Graphics2D; imp ...
- java中 awt Graphics2D
Graphics2D ,Graphics 类,提供了对几何形状.坐标转换.颜色管理和文本布局更为复杂的控制.它是用于在 Java(tm) 平台上呈现二维形状.文本和图像的基础类.验证码生成可以用到此类 ...
- Java实现打印功能-AWT Graphics2D
Java实现打印功能 用java实现打印,java.awt中提供了一些打印的API,要实现打印,首先要获得打印对象,然后继承Printable实现接口方法print,以便打印机进行打印,最后用用Gra ...
- openseadragon.js与deep zoom java实现艺术品图片展示
openseadragon.js 是一款用来做图像缩放的插件,它可以用来做图片展示,做展示的插件很多,也很优秀,但大多数都解决不了图片尺寸过大的问题. 艺术品图像展示就是最简单的例子,展示此类图片一般 ...
- java.imageIo给图片添加水印
最近项目在做一个商城项目, 项目上的图片要添加水印①,添加图片水印;②:添加文字水印; 一下提供下个方法,希望大家可以用得着: package com.blogs.image; import java ...
- 不涉及框架纯java实现将图片裁成圆形
package com.wtsrui.utils;import java.awt.Color; import sun.misc.BASE64Encoder;import java.awt.Graph ...
- java实现给图片添加水印
package michael.io.image; import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.aw ...
随机推荐
- PKU1988磁铁
Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 16258 Accepted: 5579 Ca ...
- SQLserver2008全文检索使用方法
一.开启SQL Full-text服务: 保证 SQL Full-text Filter Daemon Launcher服务处于开启状态,不同版本SQLServer全文检索服务名称可能稍有不同,如果服 ...
- Entity Framework API介绍 -- DbSet<>().Find()
过去我们常常使用Where或First(FirstOrDefault)方法来查找对应的实体,比如: var query = context.CertInfoMakeDetails.ToList().W ...
- javascript slice array to num subarray
var data = ['法国','澳大利亚','智利','新西兰','西班牙','加拿大','阿根廷','美国','0','国产','波多黎各','英国','比利时','德国','意大利','意大利 ...
- mysql 里的 ibdata1 文件不断的增长?
我们在 Percona 支持栏目经常收到关于 MySQL 的 ibdata1 文件的这个问题.当监控服务器发送一个关于 MySQL 服务器存储的报警时,恐慌就开始了 —— 就是说磁盘快要满了.一番调查 ...
- Warning: require(): open_basedir restriction in effect. File(/www/wwwroot/../thinkphp/start.php) is not within the allowed path(s):
Warning: require(): open_basedir restriction in effect. File(/www/wwwroot//../thinkphp/start.php) is ...
- python中的json的基本使用方法
在python中使用json的时候,主要也就是使用json模块,json是以一种良好的格式来进行数据的交互,从而在很多时候,可以使用json数据格式作为程序之间的接口, #!/usr/bin/env ...
- git之项目上传
git之项目上传 需求:将项目代码上传至github 前期准备: 1.github账号注册 2.安装git环境,可以打开且使用git shell. 3.生成SSH key并与github账号绑定 步骤 ...
- Halcon算子之shape_trans,用于变换区域的形状
函数原型:shape_trans(Region : RegionTrans : Type : ) *shape_trans*仍然是区域,smallest_rectangle1可以获得四个角的坐标 函数 ...
- Android apk couldn't install
an existing package with the same name and signature is already installed