图片不变形缩放裁剪

LCG454872006 2012-10-06 06:26:29
最近做一个项目用到图片的上传操作,怎么实现图片的上传缩放裁剪后图片不变形。例如将图片经过处理后变成统一的高宽比例而图片不变形。请各位高手指教。
...全文
361 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sgyyz 2012-10-08
  • 打赏
  • 举报
回复

package com.bean.tuil;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

import javax.imageio.ImageIO;

/**
* 对图片的操作
*
* @author yangyz
*
*/
public class ImageControl {
// 添加字体的属性值
private Font font = new Font("", Font.PLAIN, 12);

private Graphics2D g = null;

private int fontsize = 0;

private int x = 0;

private int y = 0;

private String destDir = "F:\\";

public void setDestDir(String path) {
this.destDir = path;
// 目录不存在
File file = new File(path);
if (!file.exists()) {
file.mkdir();
}
}

/**
* 导入本地图片到缓冲中
*
* @param imgPath
* @return
*/
public BufferedImage loadImageLocal(String imgPath) {
try {
return ImageIO.read(new File(imgPath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

/**
* 读取网络中的图片到缓冲
*
* @param imgPath
* @return
*/
public BufferedImage loadImageUrl(String imgPath) {
URL url;
try {
url = new URL(imgPath);
return ImageIO.read(url);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

/**
* 生成新图片到本地
*
* @param newImage
* 新名字
* @param img
* 缓冲
* @param type
* 类别
*/
public void writeImageLocal(BufferedImage img, String type) {
if (img != null) {
File outputFile = new File(this.destDir + new Date().getTime()
+ "." + type);
try {
ImageIO.write(img, type, outputFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

/**
* 设置文字字体,大小
*
* @param fontStyle
* @param fontSize
*/
public void setFont(String fontStyle, int fontSize) {
this.fontsize = fontSize;
this.font = new Font(fontStyle, Font.PLAIN, fontSize);
}

/**
* 单行文本
*
* @param img
* @param content
* @param x
* @param y
* @return
*/
public BufferedImage modifyImage(BufferedImage img, Object content) {
try {
int w = img.getWidth();
int h = img.getHeight();

g = img.createGraphics();
g.setColor(Color.RED);
if (this.font != null) {
g.setFont(this.font);
}

// 文字输出位置
this.y = h - fontsize - 10;
this.x = w - 70;

if (content != null) {
g.drawString(content.toString(), this.x, this.y);
}
g.dispose();
} catch (Exception e) {
e.printStackTrace();
}

return img;
}

/**
* 将两张图片合并
*
* @param base
* @param dest
* 目标
* @return
*/
public BufferedImage modifyImageTogether(BufferedImage base,
BufferedImage dest) {
try {
int w = base.getWidth();
int h = base.getHeight();

int dw = dest.getWidth();
int dh = dest.getHeight();

g = dest.createGraphics();
AlphaComposite cp = AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.3f);
g.setComposite(cp);
g.drawImage(base, dw - w, dh - h, w, h, null);
g.dispose();
} catch (Exception e) {
e.printStackTrace();
}
return dest;
}

/**
* 改变图片大小
*
* @param img
* @param weight
* @param height
* @return
*/
public BufferedImage modifySize(BufferedImage img, int width, int height) {
try {
int w = img.getWidth();
int h = img.getHeight();

double wRation = (new Integer(width)).doubleValue() / w;
double hRation = (new Integer(height)).doubleValue() / h;
Image image = img.getScaledInstance(width, height,
Image.SCALE_SMOOTH);

AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(wRation, hRation), null);

image = op.filter(img, null);

img = (BufferedImage) image;
} catch (Exception e) {
e.printStackTrace();
}

return img;
}

public BufferedImage changeSize(BufferedImage img, int width, int height) {
try {
BufferedImage distin = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
g = distin.createGraphics();
g.drawImage(img, 0, 0, width, height, null);
return distin;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/**
* 截图
*
* @param img
* @param x
* @param y
* @param width
* @param height
* @return
*/
public BufferedImage cutImage(BufferedImage img, int x, int y, int width,
int height) {
BufferedImage distin = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
try {
g = distin.createGraphics();
// 原图的大小,以及截图的坐标及大小
g.drawImage(img, 0, 0, width, height, x, y, width, height, null);
} catch (Exception e) {
e.printStackTrace();
}
return distin;
}

/**
* 测试
*
* @param args
*/
public static void main(String[] args) {
ImageControl ic = new ImageControl();

// 两张图片合并
BufferedImage d = ic
.loadImageLocal("C:\\Documents and Settings\\yangyz\\Desktop\\MyAPP\\catergory_list_right.jpg");
BufferedImage b = ic
.loadImageLocal("C:\\Documents and Settings\\yangyz\\Desktop\\MyAPP\\ico_exPlore_home_sports_bundles_selected.png");
ic.setDestDir("f:\\temp\\");
ic.writeImageLocal(ic.modifyImageTogether(b, d), "jpg");

// 在向生成的图片添加文字
ic.modifyImage(d, "Troy Young");
// // 根据坐标截图
// BufferedImage d = ic
// .loadImageLocal("C:\\Documents and Settings\\yangyz\\Desktop\\MyAPP\\catergory_list_right.jpg");
// ic.setDestDir("f:\\temp\\");
ic.writeImageLocal(ic.cutImage(d, 30, 20, 200, 100), "jpg");
// 修改图片大小
// BufferedImage d = ic
// .loadImageLocal("C:\\Documents and Settings\\yangyz\\Desktop\\MyAPP\\catergory_list_right.jpg");
// ic.setDestDir("f:\\temp\\");
ic.writeImageLocal(ic.modifySize(d, 210, 175), "jpg");

System.out.println("成功");
}
}
LCG454872006 2012-10-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
明天上班给你发代码,各种变形、剪裁!mark了
[/Quote]
谢了 给我发到454872006@qq.com邮箱吧
road_16 2012-10-07
  • 打赏
  • 举报
回复
求共享,学习了[Quote=引用 5 楼 的回复:]

明天上班给你发代码,各种变形、剪裁!mark了
[/Quote]
LCG454872006 2012-10-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
比例缩小,不足部分补上透明像素,最简单的办法!
[/Quote]

那这个怎么实现?
sgyyz 2012-10-07
  • 打赏
  • 举报
回复
明天上班给你发代码,各种变形、剪裁!mark了
jyp4470 2012-10-07
  • 打赏
  • 举报
回复
自己去看api吧
jyp4470 2012-10-07
  • 打赏
  • 举报
回复
使用drawimage将图片缩放裁剪到合适比例 在使用imagewriter将图片保存到本地就行了 或者保存到服务器
gloomyfish 2012-10-06
  • 打赏
  • 举报
回复
比例缩小,不足部分补上透明像素,最简单的办法!

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧