package com.test;

import java.awt.image.BufferedImage;
import java.io.File; import javax.imageio.ImageIO; public class Test {
private static final double WORK_LOAD_FACTOR = 0.265; private static final double LANCZOS_SUPPORT = 3;
private static final double LANCZOS_WINDOW = 3;
private static final double LANCZOS_SCALE = 1;
private static final double LANCZOS_BLUR = 1; private static final double EPSILON = 1.0e-6; private static class ContributionInfo {
private double weight;
private int pixel;
} public static BufferedImage resizeImage(BufferedImage image, double ratio) {
return resizeImage(image, (int)(image.getWidth() * ratio + 0.5), (int)(image.getHeight() * ratio + 0.5));
} public static BufferedImage resizeImage(BufferedImage image, double xRatio, double yRatio) {
return resizeImage(image, (int)(image.getWidth() * xRatio + 0.5), (int)(image.getHeight() * yRatio + 0.5));
} public static BufferedImage resizeImage(BufferedImage image, int width, int height) {
double xFactor = width * 1.0 / image.getWidth();
double yFactor = height * 1.0 / image.getHeight(); BufferedImage resizeImage = new BufferedImage(width, height, image.getType());
BufferedImage filterImage = null; if (xFactor * yFactor > WORK_LOAD_FACTOR) {
filterImage = new BufferedImage(width, image.getHeight(), image.getType());
horizontalFilter(image, filterImage, xFactor);
verticalFilter(filterImage, resizeImage, yFactor);
} else {
filterImage = new BufferedImage(image.getWidth(), height, image.getType());
verticalFilter(image, filterImage, yFactor);
horizontalFilter(filterImage, resizeImage, xFactor);
}
return resizeImage;
} private static void verticalFilter(BufferedImage image, BufferedImage resizeImage,
double yFactor) {
double scale = Math.max(1.0 / yFactor, 1.0);
double support = scale * LANCZOS_SUPPORT;
if (support < 0.5) {
support = 0.5;
scale = 1.0;
}
scale = 1.0 / scale; for (int y = 0; y < resizeImage.getHeight(); y++) {
double center = (y + 0.5) / yFactor;
int start = (int) (Math.max(center - support - EPSILON, 0.0) + 0.5);
int stop = (int) (Math.min(center + support, image.getHeight()) + 0.5);
double density = 0.0;
ContributionInfo[] contribution = new ContributionInfo[stop - start];
int n;
for (n = 0; n < (stop - start); n++) {
contribution[n] = new ContributionInfo();
contribution[n].pixel = start + n;
contribution[n].weight = getResizeFilterWeight(scale * (start + n - center + 0.5));
density += contribution[n].weight;
} if ((density != 0.0) && (density != 1.0)) {
density = 1.0 / density;
for (int i = 0; i < n; i++)
contribution[i].weight *= density;
}
for (int x = 0; x < resizeImage.getWidth(); x++) {
double red = 0;
double green = 0;
double blue = 0;
for (int i = 0; i < n; i++) {
double alpha = contribution[i].weight;
int rgb = image.getRGB(x, contribution[i].pixel);
red += alpha * ((rgb >> 16) & 0xFF);
green += alpha * ((rgb >> 8) & 0xFF);
blue += alpha * (rgb & 0xFF);
}
int rgb = roundToQuantum(red) << 16 | roundToQuantum(green) << 8
| roundToQuantum(blue);
resizeImage.setRGB(x, y, rgb);
}
}
} private static void horizontalFilter(BufferedImage image, BufferedImage resizeImage,
double xFactor) {
double scale = Math.max(1.0 / xFactor, 1.0);
double support = scale * LANCZOS_SUPPORT;
if (support < 0.5) {
support = 0.5;
scale = 1.0;
}
scale = 1.0 / scale; for (int x = 0; x < resizeImage.getWidth(); x++) {
double center = (x + 0.5) / xFactor;
int start = (int) (Math.max(center - support - EPSILON, 0.0) + 0.5);
int stop = (int) (Math.min(center + support, image.getWidth()) + 0.5);
double density = 0.0;
ContributionInfo[] contribution = new ContributionInfo[stop - start];
int n;
for (n = 0; n < (stop - start); n++) {
contribution[n] = new ContributionInfo();
contribution[n].pixel = start + n;
contribution[n].weight = getResizeFilterWeight(scale * (start + n - center + 0.5));
density += contribution[n].weight;
} if ((density != 0.0) && (density != 1.0)) {
density = 1.0 / density;
for (int i = 0; i < n; i++)
contribution[i].weight *= density;
}
for (int y = 0; y < resizeImage.getHeight(); y++) {
double red = 0;
double green = 0;
double blue = 0;
for (int i = 0; i < n; i++) {
double alpha = contribution[i].weight;
int rgb = image.getRGB(contribution[i].pixel, y);
red += alpha * ((rgb >> 16) & 0xFF);
green += alpha * ((rgb >> 8) & 0xFF);
blue += alpha * (rgb & 0xFF);
}
int rgb = roundToQuantum(red) << 16 | roundToQuantum(green) << 8
| roundToQuantum(blue);
resizeImage.setRGB(x, y, rgb);
}
}
} private static double getResizeFilterWeight(double x) {
double blur = Math.abs(x) / LANCZOS_BLUR;
double scale = LANCZOS_SCALE / LANCZOS_WINDOW;
scale = sinc(blur * scale);
return scale * sinc(blur);
} private static double sinc(double x) {
if (x == 0.0)
return 1.0;
return Math.sin(Math.PI * x) / (Math.PI * x);
} private static int roundToQuantum(double value) {
if (value <= 0.0)
return 0;
if (value >= 255)
return 255;
return (int) (value + 0.5);
} public static void main(String[] args) throws Exception {
BufferedImage image = ImageIO.read(new File("C:/Users/admin/Desktop/efg.jpg"));
ImageIO.write(resizeImage(image, 100, 100), "PNG", new File("C:/Users/admin/Desktop/sefg.jpg"));
} }

Java 裁剪图片的更多相关文章

  1. java裁剪图片

    java裁剪图片保存到指定位置 /** * 图片裁剪通用接口 * * @param src 源图片地址,图片格式PNG * @param dest 目的图片地址 * @param x 图片起始点x坐标 ...

  2. 【转】java缩放图片、java裁剪图片代码工具类

    一首先看下效果 二工具类 三测试类 在系统的上传图片功能中,我们无法控制用户上传图片的大小,用户可能会上传大到几十M小到1k的的图片,一方面图片太大占据了太多的空间,另一方面,我们没办法在页面上显示统 ...

  3. Java+jquery实现裁剪图片上传到服务器

    大体分两步: 1.利用jquery裁剪图片,把裁剪到的几个点传入后端 2.利用前端传入的几个点,来裁剪图片 首先,用到一个jquery的插件 imgAreaSelect 实例及插件下载地址:http: ...

  4. java多图片上传--前端实现预览--图片压缩 、图片缩放,区域裁剪,水印,旋转,保持比例。

    java多图片上传--前端实现预览 前端代码: https://pan.baidu.com/s/1cqKbmjBSXOhFX4HR1XGkyQ 解压后: java后台: <!--文件上传--&g ...

  5. JAVA实现图片裁剪

    /** * 裁剪图片 * @param src 源图片 * @param dest 裁剪后的图片 * @param x 裁剪范围的X坐标 * @param y 裁剪范围的Y坐标 * @param w ...

  6. JAVA生成图片缩略图、JAVA截取图片局部内容

    package com.ares.image.test; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; ...

  7. java改变图片文件尺寸

    package test.common; import java.awt.Graphics; import java.awt.Image; import java.awt.image.Buffered ...

  8. 如何兼容所有Android版本选择照片或拍照然后裁剪图片--基于FileProvider和动态权限的实现

    我们知道, Android操作系统一直在进化. 虽然说系统是越来越安全, 可靠, 但是对于开发者而言, 开发难度是越来越大的, 需要注意的兼容性问题, 也越来越多. 就比如在Android平台上拍照或 ...

  9. cropper(裁剪图片)插件使用(案例)

    公司发布微信H5前端阵子刚刚弄好的H5端的图片上传插件,现在有需要裁剪图片.前端找了一个插件---cropper 本人对这插件不怎么熟悉,这个案例最好用在一个页面只有一个上传图片的功能上而且只适合单个 ...

随机推荐

  1. Python 实现WC功能

    GitHub仓库:https://github.com/15crmor/PAC 项目要求 基本要求 -c 统计文件字符数 (实现) -w 统计文件词数 (实现) -l 统计文件行数(实现) 扩展功能 ...

  2. Java内存模型以及Volatile、Synchronize关键字的疑问

    1.众所周知,java的内存模型是一个主内存,每个线程都有一个工作内存空间,那么主内存同步到工作内存是什么时候发生的呢?工作内存同步会主内存又是什么时候发生的呢? 在cpu进行线程切换时就会发生这些同 ...

  3. EBS Webservice Timeout,HTTP Server Return "500 Internal Server Error"

    http://blog.itpub.net/26687597/viewspace-1207571/ 基于Oracle EBS R12,开发了一个Webservice用于返回某项主数据,当请求的数据量非 ...

  4. 基于tinyproxy搭建代理服务器

    在我们实际的工作当中,经常会遇到这种情况,我们对线上服务器进行操作时是通过跳板机来进行的,出于安全性及投入资金来考虑非必要情况下除跳板机以外的服务器是没有内网ip的,所以当我们位于内网的服务器需要使用 ...

  5. LeetCode135:Candy

    题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...

  6. Spring Boot 2 实践记录之 Redis 及 Session Redis 配置

    先说 Redis 的配置,在一些网上资料中,Spring Boot 的 Redis 除了添加依赖外,还要使用 XML 或 Java 配置文件做些配置,不过经过实践并不需要. 先在 pom 文件中添加 ...

  7. HttpWebRequest 模拟浏览器访问网站

    最近抓网页时报错: 要么返回 The remote server returned an error: (442)要么返回: 非法访问,您的行为已被WAF系统记录! 想了想,就当是人家加了抓网页的东西 ...

  8. SSRS (一)创建基础报表

    ReportService创建基础报表 1.数据库SQL Server2012选择SQL Server Data Tools 2.创建商业智能(BI)项目 选择报表服务器项目 ReportServic ...

  9. pageadmin CMS网站制作教程:模板概念解释

    pageadmin CMS网站建设教程:模板概念解释 1.模板页 又叫视图页面,PageAdmin后台栏目或信息中用到的模板页面的统称,格式必须是.cshtml后缀文件,前端人员制作的页面默认都是ht ...

  10. leetcode 123. 买卖股票的最佳时机 III JAVA

    题目: 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你最多可以完成 两笔 交易. 注意: 你不能同时参与多笔交易(你必须在再次购买前出 ...