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. Android-系统解析AndroidManifest

    在上一篇博客,Android-XML格式描述,介绍来XML在Android中的格式: Android-系统解析AndroidManifest,是分析Android系统是如何去解析AndroidMani ...

  2. c#中取整方式

    主要用到 System 命名空间下的一个数据类 Math ,调用他的方法 一共有三种方式: 第一种 Math.Round:根据四舍五入取整 第二种 Math.Ceiling:向上取整,有小数,整数加1 ...

  3. C#跨线程操作UI

    WPF启动调度器 : Dispatcher.Invoke(new Action(() => { //你的代码 }));

  4. log Log4NET配置

    Log4Net是用来记录日志的,可以将程序运行过程中的信息输出到一些地方(文件.数据库.EventLog等),日志就是程序的黑匣子,可以通过 日志查看系统的运行过程,从而发现系统的问题.日志的作用:将 ...

  5. 用ndp部署storm应用

    本文由作者余宝虹授权网易云社区发布. 使用户ndp部署一个Java应用大家都非常熟悉的,但是看到某些同学用非常繁琐的方式部署storm应用的时候,我觉得很有必要整一个帮助教程,ndp帮助文档里面没有, ...

  6. Java基础学习篇---------this、object的学习

    一.this的学习方法 1.使用this调用构造方法市一定放在构造方法的首行 2.使用this调用构造方法时一定流出调用的出口 public class MyClass { public MyClas ...

  7. Android 美学设计基础 <3>

    本期接着对Android的美学设计的分享. 1.3 Light and shadows 光学与阴影 1.3.1 Light 在素材设计的环境中,我们会用虚拟的光来照亮UI界面.主灯光会产生尖锐,有方向 ...

  8. setInterval传递参数

    参照:https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout 平时我们使用定时器的时 ...

  9. jquery, jquery-ui, requirejs, bootstrap 的关系理解

    jquery 是 基于 javascript 的一个语法衍生,更方便操作dom, 事件,css 整体来说更好用,更简洁. jquery-ui 是基于 jquery 实现的各种更好看,交互更友好的的界面 ...

  10. 用c语言实现三子棋

    1 game.c://实现三子棋的.c文件 #define _CRT_SECURE_NO_WARNINGS #include"game.h" void init_board(cha ...