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. OpenCV中GPU函数

    The OpenCV GPU module is a set of classes and functions to utilize GPU computational capabilities. I ...

  2. SQL SERVER存储过程中使用事务与捕获异常

    https://www.douban.com/note/559596669/ 格式类似于 CREATE PROCEDURE YourProcedure ASBEGIN    SET NOCOUNT O ...

  3. RabbitMQ 消息队列 DEMO

    1. 引用 RabbitMQ.Client.5.1.0 2. http://localhost:15672/ public class TestController : ApiController { ...

  4. C#委托、事件剖析(上)

    本节对委托.事件做以总结. 一.委托: 1.概念:先来说明变量和函数的概念,变量,以某个地址为起点的一段内存中所存储的值,函数,以某个地址为起点的一段内存中存储的机器语言指令.有了这2个概念以后,我们 ...

  5. Unity Shader序列帧动画学习笔记

    Unity Shader序列帧动画学习笔记 关于无限播放序列帧动画的一点问题 在学shader的序列帧动画时,书上写了这样一段代码: fixed4 frag(v2f i){ // 获得整数时间 flo ...

  6. leetcode 最后一个单词的长度

    给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 说明:一个单词是指由字母组成,但不包含任何空格的字符串. 示例: 输入: &quo ...

  7. vs 生成事件 +版本号+sed.exe

    set ASMINFO=Properties\AssemblyInfo.csFINDSTR /C:"[assembly: AssemblyVersion(" %ASMINFO% | ...

  8. c# 锁 Interlocked 操作

    //定义原子变量 ; //原子级别+1值,如果>=0,说明当前锁为空,可以执行,避免重复执行 ) { if (_serverThread == null || (_serverThread.Is ...

  9. IntelliJ IDEA优秀插件(编程通用)

    一.IntelliJ IDEA开发 最近大部分开发IDE工具都切换到了,所以也花了点心思去找了相关的插件.这里整理的适合各种语言开发的通用插件,也排除掉IntelliJ IDEA自带的常用插件了(有些 ...

  10. nginx代理websocket协议

    以下是代码段.location /wsapp/ {     proxy_pass http://wsbackend;     proxy_http_version 1.1;     proxy_set ...