怎样把图片按比例缩小 java实现 各位大哥,小弟遇到难题了。我想把800*600的图片缩小为80*60的,不知道怎么用java实现,算法不用太好,只要能转换后能看得清楚图片,失真不要太厉害就行了。请各位高手帮帮忙,小弟感激不尽! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 以前项目中用过的方法,按原图比例缩放!import com.sysdynamic.interfaces.IGetScaledImage;import com.sysdynamic.util.JOptionPanes;import java.awt.Graphics2D;import java.awt.Image;import java.awt.RenderingHints;import java.awt.image.BufferedImage;import javax.swing.ImageIcon;/** * * 缩放图片 * * @author Andy Yang * @email yun.8610252163.com */public class GetScaledImage implements IGetScaledImage { public static int snapHeightMax = 120; // 缩放后最小高度 public static int snapWidthMax = 150; // 缩放后最小宽度 /** * 无参构造方法 */ public GetScaledImage() { } /** * 根据图片,缩放比例得到缩略图 * * @param img * 源图片 * @param scaled * 缩放比例 * @return 放缩scaled倍后的缩略图 */ public static ImageIcon getImage(Image img, int scaled) { ImageIcon icon = new ImageIcon(getImages(img, scaled)); return icon; } /** * 根据图片,缩放比例得到图片 * * @param img * 源图片 * @param scaled * 缩放比例 * @return 放缩scaled倍后的图片 */ public static Image getImages(Image img, int scaled) { /* * Double w = 7.5 * scaled / 2; Double h = 5.0 * scaled / 2; int width = * w.intValue(); int heigth = h.intValue(); if (width < 150 || heigth < * 100) { width = 150; heigth = 100; } */ // 2008-04-24 int heigth = 0; int width = 0; if (scaled < 25) { scaled = 25; } String sScaled = String.valueOf(Math.ceil((double) scaled / 25)); int indexX = sScaled.indexOf("."); scaled = Integer.parseInt(sScaled.substring(0, indexX)); double scaleds = getScaling(img.getWidth(null), img.getHeight(null), scaled); try { heigth = (int) (img.getHeight(null) * scaleds); width = (int) (img.getWidth(null) * scaleds); //System.out.println("heigth " + heigth + "width " // + width); } catch (Exception e) { JOptionPanes.showPlainMessage("Pixel picture too much !"); //这里要改掉. } return getScaledImage(img, width, heigth); } /** * 根据图片,缩放目标图片高,宽,得到图片 * * @param srcImg * 源图片 * @param width * 缩放目标图片宽 * @param h * 缩放目标图片高 * @return 放缩后宽为width,高为heigth的图片 */ public static Image getScaledImage(Image srcImg, int width, int heigth) { BufferedImage resizedImg = new BufferedImage(width, heigth, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = resizedImg.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(srcImg, 0, 0, width, heigth, null); g2.dispose(); Image image = srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_DEFAULT); return resizedImg; } /** * 得到图像缩放比率 * * @param sourceWidth * 源图宽 * @param sourceHeight * 源图高 * @return double 缩放比率 */ public static double getScaling(int sourceWidth, int sourceHeight, int scaled) { double widthScaling = ((double) snapWidthMax * (double) scaled) / (double) sourceWidth; double heightScaling = ((double) snapHeightMax * (double) scaled) / (double) sourceHeight; double scaling = (widthScaling < heightScaling) ? widthScaling : heightScaling; return scaling; }} java 异常处理问题。谢谢大家了 用ultraedit写.java的时候,如何不让string自动变成String? 数据库sql问题 snmp 如何读取文件?急! 有个简单问题请各位大哥帮忙, 像visio那樣圖形元素之間可以隨意移動,連接如何用java實現 jbuilder有没有类似visual assist插件? 关于javamail的问题,清高首解答 请教一下算法第四版中的课后习题问题 java中实现按“enter”键登陆 求助概念问题,关于基本数据类型与他们的 封装类的不同.....
以前项目中用过的方法,按原图比例缩放!
import com.sysdynamic.interfaces.IGetScaledImage;import com.sysdynamic.util.JOptionPanes;import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;import javax.swing.ImageIcon;
/**
*
* 缩放图片
*
* @author Andy Yang
* @email yun.8610252163.com
*/
public class GetScaledImage implements IGetScaledImage {
public static int snapHeightMax = 120; // 缩放后最小高度
public static int snapWidthMax = 150; // 缩放后最小宽度 /**
* 无参构造方法
*/
public GetScaledImage() {
} /**
* 根据图片,缩放比例得到缩略图
*
* @param img
* 源图片
* @param scaled
* 缩放比例
* @return 放缩scaled倍后的缩略图
*/
public static ImageIcon getImage(Image img, int scaled) {
ImageIcon icon = new ImageIcon(getImages(img, scaled)); return icon;
} /**
* 根据图片,缩放比例得到图片
*
* @param img
* 源图片
* @param scaled
* 缩放比例
* @return 放缩scaled倍后的图片
*/
public static Image getImages(Image img, int scaled) {
/*
* Double w = 7.5 * scaled / 2; Double h = 5.0 * scaled / 2; int width =
* w.intValue(); int heigth = h.intValue(); if (width < 150 || heigth <
* 100) { width = 150; heigth = 100; }
*/ // 2008-04-24
int heigth = 0;
int width = 0; if (scaled < 25) {
scaled = 25;
} String sScaled = String.valueOf(Math.ceil((double) scaled / 25));
int indexX = sScaled.indexOf(".");
scaled = Integer.parseInt(sScaled.substring(0, indexX)); double scaleds = getScaling(img.getWidth(null), img.getHeight(null),
scaled); try {
heigth = (int) (img.getHeight(null) * scaleds);
width = (int) (img.getWidth(null) * scaleds); //System.out.println("heigth " + heigth + "width "
// + width);
} catch (Exception e) {
JOptionPanes.showPlainMessage("Pixel picture too much !"); //这里要改掉.
} return getScaledImage(img, width, heigth);
} /**
* 根据图片,缩放目标图片高,宽,得到图片
*
* @param srcImg
* 源图片
* @param width
* 缩放目标图片宽
* @param h
* 缩放目标图片高
* @return 放缩后宽为width,高为heigth的图片
*/
public static Image getScaledImage(Image srcImg, int width, int heigth) {
BufferedImage resizedImg = new BufferedImage(width, heigth,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, width, heigth, null);
g2.dispose(); Image image = srcImg.getScaledInstance(srcImg.getWidth(null),
srcImg.getHeight(null), Image.SCALE_DEFAULT); return resizedImg;
} /**
* 得到图像缩放比率
*
* @param sourceWidth
* 源图宽
* @param sourceHeight
* 源图高
* @return double 缩放比率
*/
public static double getScaling(int sourceWidth, int sourceHeight,
int scaled) {
double widthScaling = ((double) snapWidthMax * (double) scaled) / (double) sourceWidth;
double heightScaling = ((double) snapHeightMax * (double) scaled) / (double) sourceHeight; double scaling = (widthScaling < heightScaling) ? widthScaling
: heightScaling; return scaling;
}
}