/**
* 根据scale来放大或缩小图像!
*/
public static void scale(String source, String result, int scale,
boolean flag) {try {
BufferedImage src = ImageIO.read(new File(source)); // 读入文件
int width = src.getWidth(); // 得到源图宽
int height = src.getHeight(); // 得到源图长
if (flag) {
// 放大
width = width * scale;
height = height * scale;
} else {
// 缩小
width = width / scale;
height = height / scale;
}
Image image = src.getScaledInstance(width, height,
Image.SCALE_DEFAULT);
//
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
// // Graphics context no longer needed so dispose it
g.dispose();
ImageIO.write(tag, "JPEG", new File(result));// 输出到文件流} catch (IOException e) {
e.printStackTrace();
}
}/**
* 调整图像到指定尺寸!
*
* @param source
* @param result
* @param width
* @param height
*/
public static void scale(String source, String result, int width, int height) {try {
BufferedImage src = ImageIO.read(new File(source)); // 读入文件
Image image = src.getScaledInstance(width, height,
Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
//
double Ratio = 0.0;
if ((tag.getHeight() > 120) || (tag.getWidth() > 120)) {
if (tag.getHeight() > tag.getWidth())
Ratio = 120.0 / tag.getHeight();
else
Ratio = 120.0 / tag.getWidth();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(Ratio, Ratio), null);
tag = op.filter(tag, null);
//
// tag.getGraphics().drawImage(image, 0, 0, null); // 绘制缩小后的图ImageIO.write(tag, "JPEG", new File(result));// 输出到文件流} catch (IOException e) {
e.printStackTrace();
}
}
* 根据scale来放大或缩小图像!
*/
public static void scale(String source, String result, int scale,
boolean flag) {try {
BufferedImage src = ImageIO.read(new File(source)); // 读入文件
int width = src.getWidth(); // 得到源图宽
int height = src.getHeight(); // 得到源图长
if (flag) {
// 放大
width = width * scale;
height = height * scale;
} else {
// 缩小
width = width / scale;
height = height / scale;
}
Image image = src.getScaledInstance(width, height,
Image.SCALE_DEFAULT);
//
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
// // Graphics context no longer needed so dispose it
g.dispose();
ImageIO.write(tag, "JPEG", new File(result));// 输出到文件流} catch (IOException e) {
e.printStackTrace();
}
}/**
* 调整图像到指定尺寸!
*
* @param source
* @param result
* @param width
* @param height
*/
public static void scale(String source, String result, int width, int height) {try {
BufferedImage src = ImageIO.read(new File(source)); // 读入文件
Image image = src.getScaledInstance(width, height,
Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
//
double Ratio = 0.0;
if ((tag.getHeight() > 120) || (tag.getWidth() > 120)) {
if (tag.getHeight() > tag.getWidth())
Ratio = 120.0 / tag.getHeight();
else
Ratio = 120.0 / tag.getWidth();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(Ratio, Ratio), null);
tag = op.filter(tag, null);
//
// tag.getGraphics().drawImage(image, 0, 0, null); // 绘制缩小后的图ImageIO.write(tag, "JPEG", new File(result));// 输出到文件流} catch (IOException e) {
e.printStackTrace();
}
}
解决方案 »
- 我又来送分了!!! 搞不懂 equal symbol expected 这个错误的原因.
- 关于webwork和freemarker的问题
- 小弟求一JDBC-ODBC远程访问SQL的代码
- 毕业设计文件比较分析的问题的问题~急~最后的100分在线等待
- 烟草行业软件公司急招JAVA高级工程师/系统分析员
- 在用IIS+RESIN3.0.8的兄弟进来
- JDevelop 9.0.5 出来了,哪儿可以下载??
- 请教大侠们,急!!!!!
- 大家帮我看一下,这段代码是否能够链上数据库呢?
- petstore中为什么可以有.screen的页面的?为什么能认这种格式并自动从screendefinitions_en_US.xml查找到对应的程序?
- weblogic中配置数据源(Oracle)问题,在线等答案!!!
- 关于打开mht文件(java转向到一个mht文件网面时出了问题)
謝謝你,在線等
另外,从数据库读图片,看看API好像可以用
BufferedImage bi=ImageIO.read(InputStream input)
试试用ByteArrayInputStream