- hf200012
- 等级:
- 文章: 30
- 积分: 148
|
这个是在参考网上一些代码基础上进行编写的,主要是首先实现图片的等比缩放,然后在进行截取,比如一张1024*768的图要截成263*150的,首先是等比缩放后变成263*197的,然后在对这个图片的高度进行截取,变成163*150的
java 代码
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Rectangle;
- import java.awt.RenderingHints;
- import java.awt.geom.AffineTransform;
- import java.awt.image.BufferedImage;
- import java.awt.image.ColorModel;
- import java.awt.image.WritableRaster;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import javax.imageio.ImageIO;
- import com.sun.image.codec.jpeg.JPEGCodec;
- import com.sun.image.codec.jpeg.JPEGImageEncoder;
-
-
-
-
-
-
-
- public class ImageHepler {
-
-
-
-
-
-
-
- private static BufferedImage resize(BufferedImage source, int targetW,
- int targetH) {
-
- int type = source.getType();
- BufferedImage target = null;
- double sx = (double) targetW / source.getWidth();
- double sy = (double) targetH / source.getHeight();
-
-
- if (sx < sy) {
- sx = sy;
- targetW = (int) (sx * source.getWidth());
- } else {
- sy = sx;
- targetH = (int) (sy * source.getHeight());
- }
- if (type == BufferedImage.TYPE_CUSTOM) {
- ColorModel cm = source.getColorModel();
- WritableRaster raster = cm.createCompatibleWritableRaster(targetW,
- targetH);
- boolean alphaPremultiplied = cm.isAlphaPremultiplied();
- target = new BufferedImage(cm, raster, alphaPremultiplied, null);
- } else
- target = new BufferedImage(targetW, targetH, type);
- Graphics2D g = target.createGraphics();
-
- g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
- RenderingHints.VALUE_INTERPOLATION_BICUBIC);
- g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
- g.dispose();
- return target;
- }
-
-
-
-
-
-
-
-
-
-
-
- public static void saveImageAsJpg(String inFilePath, String outFilePath,
- int width, int hight, boolean proportion)throws Exception {
- File file = new File(inFilePath);
- InputStream in = new FileInputStream(file);
- File saveFile = new File(outFilePath);
-
- BufferedImage srcImage = ImageIO.read(in);
- if (width > 0 || hight > 0) {
-
- int sw = srcImage.getWidth();
- int sh = srcImage.getHeight();
-
- if (sw > width && sh > hight) {
- srcImage = resize(srcImage, width, hight);
- } else {
- String fileName = saveFile.getName();
- String formatName = fileName.substring(fileName
- .lastIndexOf('.') + 1);
- ImageIO.write(srcImage, formatName, saveFile);
- return;
- }
- }
-
- int w = srcImage.getWidth();
- int h = srcImage.getHeight();
-
- if (w == width) {
-
- int x = 0;
- int y = h / 2 - hight / 2;
- saveSubImage(srcImage, new Rectangle(x, y, width, hight), saveFile);
- }
-
- else if (h == hight) {
-
- int x = w / 2 - width / 2;
- int y = 0;
- saveSubImage(srcImage, new Rectangle(x, y, width, hight), saveFile);
- }
- in.close();
- }
-
-
-
-
-
-
-
- private static void saveSubImage(BufferedImage image,
- Rectangle subImageBounds, File subImageFile) throws IOException {
- if (subImageBounds.x < 0 || subImageBounds.y < 0
- || subImageBounds.width - subImageBounds.x > image.getWidth()
- || subImageBounds.height - subImageBounds.y > image.getHeight()) {
- System.out.println("Bad subimage bounds");
- return;
- }
- BufferedImage subImage = image.getSubimage(subImageBounds.x,subImageBounds.y, subImageBounds.width, subImageBounds.height);
- String fileName = subImageFile.getName();
- String formatName = fileName.substring(fileName.lastIndexOf('.') + 1);
- ImageIO.write(subImage, formatName, subImageFile);
- }
-
-
- }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|
- loky
- 等级: 初级会员
- 性别:
- 文章: 10
- 积分: 50
- 来自: 上海
|
图片上传和等比缩放的技术我刚刚在一个项目中用过,
和你用的差不多,不过很是费了一番周折。希望能对以后的朋友有些帮助。
|
返回顶楼 |
|
|
- form_rr
- 等级: 初级会员
- 性别:
- 文章: 55
- 积分: 0
- 来自: 珠海
|
透明的png图片缩放后的图片就不透明了!
怎么解决啊?
|
返回顶楼 |
|
|
- likehibernate
- 等级: 初级会员
- 性别:
- 文章: 19
- 积分: 56
- 来自: 厦门
|
只能缩小不能放大啊!
|
返回顶楼 |
|
|