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. Beyond Compare脚本:命令行批量比较文件并生成html格式的差异报告

    BComp.exe /silent /closescript /solo @E:\compareTest\BCbatch.txt text-report layout:side-by-side opt ...

  2. (zxing.net)一维码MSI的简介、实现与解码

    一.简介 MSI/Plessey 条码(也被称为 MSI 或 Modified Plessey)是一款数字条码,多用于超市.存储用的仓库和其他贮藏室的货架.货架上的条码可以告知货架上的产品.应放数量和 ...

  3. DataTable根据字段去重

    DataTable根据字段去重 最近需要对datatable根据字段去重,在网上搜了很多,找到了一个方法,代码如下 public static DataTable DistinctSomeColumn ...

  4. 转(C# 实现生产者消费者队列)

    class Program { // 任务队列 static Queue<string> _tasks = new Queue<string>(); // 为保证线程安全,使用 ...

  5. JQuery - Ajax和Tomcat跨域请求问题解决方法!

    在JQuery里面使用Ajax和Tomcat服务器之间进行数据交互,遇到了跨域请求问题,无法成功得到想要的数据! 错误信息部分截图: 通过错误信息判断知道已经发生在Ajax跨域请求问题了! 当前Tom ...

  6. CentOS 6 - 升级内核

    有的时候,需要升级Linux内核,今天我就是在CentOS 6中升级内核,在没有升级内核之前,我的CentOS 6只有2.6.32这一个内核,也是默认启动的内核.下面就开始一步步操作升级内核了! 一, ...

  7. umeng推送, 生产环境deviceToken失效可能原因

    1 在系统升级之后会造成app的deviceToken重置(一定). 2 在app卸载之后可能会造成app的deviceToken重置. 3 deviceToken重置使用umeng推送时会因为dev ...

  8. js string 和 json 互转

    var o = JSON.parse('{"a": 8}'); JSON. stringify(o);

  9. Android逆向进阶——让你自由自在脱壳的热身运动(dex篇)

    本文作者:HAI_ 0×00 前言 来看看我们今天的主题. 让你自由自在脱壳的热身运动. 现在很多第厂家都是使用第三方的加固方式来进行加固的.或者使用自己的加固方式进行加固. 那么我们必不可少的就是脱 ...

  10. [Swift实际操作]七、常见概念-(4)范围CGRect的使用详解

    本文将为你演示区域对象CGRect的使用.你可以将区域对象,看作是点对象和尺寸对象的组合 首先导入需要使用到底界面工具框架 import UIKit 然后初始化一个区域对象,它的原点位于(0,0),宽 ...