图片不变形缩放裁剪

LCG454872006 2012-10-06 06:26:29
最近做一个项目用到图片的上传操作,怎么实现图片的上传缩放裁剪后图片不变形。例如将图片经过处理后变成统一的高宽比例而图片不变形。请各位高手指教。
...全文
404 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
  • 打赏
  • 举报
回复
比例缩小,不足部分补上透明像素,最简单的办法!
内容概要:本文介绍了一种基于多目标粒子群算法(MOPSO)的微电网优化调度模型,综合考虑风能、光伏、储能系统、柴油发电机、燃气轮机以及与主电网之间的能量交互等多种分布式能源的协同运行。通过构建以运行成本最小化、碳排放最低化和系统可靠性最优化为目标的多目标优化模型,利用Matlab平台实现MOPSO算法求解,完成对微电网在不同运行场景下的能量管理与调度方案优化。该模型能够有效平衡经济性与环保性之间的关系,适用于含多类型分布式电源的复杂微电网系统,具有较强的工程应用价值和科研参考意义; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及工程技术人员,尤其适合从事微电网、智能电网、综合能源系统、可再生能源集成与优化调度等领域研究的专业人士; 使用场景及目标:①用于多能源耦合微电网系统的协同优化调度研究;②支持多目标智能优化算法在能源系统中的建模与求解实践,帮助用户掌握MOPSO在实际工程问题中的应用方法;③为学术论文复现、毕业设计、科研项目开发提供完整的代码实例与技术支撑; 阅读建议:建议读者结合Matlab代码与理论文档,深入理解目标函数构建、约束条件处理及Pareto最优解集生成机制,重点关注算法参数设置、多目标权衡分析与结果可视化,并可通过调整能源配置或引入新约束进行二次开发与创新研究。

81,111

社区成员

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

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