zsjstart

View Post

java-如何改变图片大小。

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class JpgChange {

    // path 路径 ,旧文件名称 ,新文件名称,n 改变倍数

    public void changeImage(String path, String oldimg, String newimg, int n) {

       try {

           File file = new File(path + oldimg);

           Image img = ImageIO.read(file);

           // 构造Image对象

           int wideth = img.getWidth(null); // 得到源图宽

           int height = img.getHeight(null); // 得到源图长

           BufferedImage tag = new BufferedImage(n * wideth, n * height,

                  BufferedImage.TYPE_INT_RGB);

           tag.getGraphics()

                  .drawImage(img, 0, 0, n * wideth, n * height, null); // 绘制后的图

           FileOutputStream out = new FileOutputStream(path + newimg);

           JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

           encoder.encode(tag); // 近JPEG编码

           out.close();

       } catch (IOException e) {

           System.out.println("处理文件出现异常");

           e.printStackTrace();

       }

    }

    public static void main(String[] args) {

       JpgChange jc = new JpgChange();

       jc.changeImage("E:\\", "1.jpg", "2.jpg", 3);

    }

}

分类:

技术点:

相关文章:

  • 2022-01-02
  • 2021-06-22
  • 2021-10-30
  • 2021-03-28
  • 2021-04-24
  • 2022-02-17
猜你喜欢
  • 2021-12-26
  • 2022-12-23
  • 2021-11-23
  • 2021-12-31
  • 2022-02-15
  • 2021-12-10
  • 2022-03-09
相关资源
相似解决方案