[b]背景:[/b]原始图片宽高比例不能适应 新的页面需要,需要重新调整, 而目前除了使用 专业的图片工具之外, 也可以使用Java 自动处理,处理后的效果同 photoshop 几乎没有区别.
[b]代码片段:[/b]
BufferedImage in = ImageIO.read(new URL(url));
//使用3字节图片编码
BufferedImage ut = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);// TYPE_4BYTE_BGR);
Graphics g = out.getGraphics();
//填白底
g.setColor(Color.white);
g.fillRect(0, 0, w, h);
int h1 = in.getHeight();
int w1 = in.getWidth();
float fh = ((float) h1) / h;
float fw = ((float) w1) / w;
int h = 0;
int w = 0;
if (fh > fw) {
int w2 = (int) (w1/fh);
ow = (w - w2)/2;
w = w2;
} else {
int h2 = (int) (h1/fw);
oh = (h - h2)/2;
h = h2;
}
//按比例缩/放原始图片
Image tmp = in.getScaledInstance(w, h, Image.SCALE_SMOOTH);
g.drawImage(tmp, ow, oh, w, h, null);
ImageIO.write(out, "jpg", new File(file));
[b]代码片段:[/b]
BufferedImage in = ImageIO.read(new URL(url));
//使用3字节图片编码
BufferedImage ut = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);// TYPE_4BYTE_BGR);
Graphics g = out.getGraphics();
//填白底
g.setColor(Color.white);
g.fillRect(0, 0, w, h);
int h1 = in.getHeight();
int w1 = in.getWidth();
float fh = ((float) h1) / h;
float fw = ((float) w1) / w;
int h = 0;
int w = 0;
if (fh > fw) {
int w2 = (int) (w1/fh);
ow = (w - w2)/2;
w = w2;
} else {
int h2 = (int) (h1/fw);
oh = (h - h2)/2;
h = h2;
}
//按比例缩/放原始图片
Image tmp = in.getScaledInstance(w, h, Image.SCALE_SMOOTH);
g.drawImage(tmp, ow, oh, w, h, null);
ImageIO.write(out, "jpg", new File(file));