Java 裁剪图片


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 裁剪图片

时间: 2024-07-30 03:13:22

Java 裁剪图片的相关文章

Java实现图片裁剪预览功能

Java实现图片裁剪预览功能 在项目中,我们需要做些类似头像上传,图片裁剪的功能,ok看下面文章! 需要插件:jQuery Jcrop 后端代码: package org.csg.upload; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import 

JAVA实现图片裁剪

1 /** 2 * 裁剪图片 3 * @param src 源图片 4 * @param dest 裁剪后的图片 5 * @param x 裁剪范围的X坐标 6 * @param y 裁剪范围的Y坐标 7 * @param w 裁剪范围的高度 8 * @param h 裁剪范围的宽度 9 * @param destW 裁剪后图片的宽度 10 * @param destH 裁剪后图片的高度 11 * @throws IOException 12 */ 13 public static void c

Android拍照、相册选取、裁剪图片

来自:http://blog.csdn.net/ryantang03/article/details/8656278 package com.example.listactivity; import java.io.ByteArrayOutputStream; import java.io.File; import com.example.model.ImageTools; import android.app.Activity; import android.app.AlertDialog;

hbuilder mui调用系统裁剪图片、头像裁剪-Android

head.addEventListener('tap', function() { var IMAGE_UNSPECIFIED = "image/*"; //相册显示的文件类型 var PHOTOZOOM = 2; // 获取完图片返回key var PHOTOLAT = 1; // 剪裁完毕后返回key var main = plus.android.runtimeMainActivity(); //h5+获取应用主Activity实例对象 var Intent = plus.and

Unity3d本地上传并且裁剪图片-----Android平台

注:引擎版本unity4.x 最近项目需求,需要做用户头像,要求: 1.  可以从本地上传 2.  本地裁剪 3.  压缩控制大小 4.  在三个平台实现PC/Android/IOS 弄了好几天总是搞完了总结一下 从本地上传会用到Android系统功能,打开相册和用摄像机拍照,因此unity和Android的交互是必须要会的. Unity-android可参考到宣雨松的博客http://www.xuanyusong.com/archives/676package com.cheerflame.s

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

package com.ares.image.test; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Arrays; import javax.imageio.ImageIO; import o

Android调用相机实现拍照并裁剪图片,调用手机中的相冊图片并裁剪图片

在 Android应用中,非常多时候我们须要实现上传图片,或者直接调用手机上的拍照功能拍照处理然后直接显示并上传功能,以下将讲述调用相机拍照处理图片然后显示和调用手机相冊中的图片处理然后显示的功能,要想实现上传功能.一般都是上传到数据库中,将imageView中的图片取出来然后存到数据库中就可以. 以下讲述实现的步骤: 1. 调用相冊中的图片裁剪然后显示. 1.1 使用Intent获取从相冊中选择的照片. 1.2 对获取的图片进行裁剪处理.裁剪处理也是使用Intent调用的Android自带的裁

自定义裁剪图片

判断图片大小,根据裁剪目标对图片进行缩放,然后裁剪图片(在图像不变形情况下) /// <summary> /// 指定长宽裁剪 /// 按模版比例最大范围的裁剪图片并缩放至模版尺寸 /// </summary> /// <param name="fromFile">原图Stream对象</param> /// <param name="fileSaveUrl">保存路径</param> ///

Quartz2D练习 -- 裁剪图片分类

Main.storyboard <?xml version="1.0" encoding="UTF-8" standalone="no"?> <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="5053" systemVers