• java图片转换格式并设定大小


    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    /**
    * 支持转换成BMP, JPG,PNG, JPEG, WBMP, GIF格式
    *
    */
    public class ImageConvertJPEG {
    private String inPath; //输入路径
    private String outPath; //输出路径,不包含文件名,如C:\\Users\\Administrator\\Desktop或C:/Users/Administrator/Desktop
    private String outFileName; //输出文件名,如myPicture
    private int width=200; //转换后的图片宽度
    private int heigth=200; //轮换后的图片高度

    public void beginConvert() throws IOException {
    File inFile
    = new File(inPath);
    if (!inFile.exists() || inFile.isDirectory()) {
    throw new IOException("文件不存在!");
    }
    File outFile
    = new File(outPath + "/" + outFileName + ".jpeg");
    try {
    BufferedImage bufferedImage
    = ImageIO.read(inFile);
    BufferedImage bufferedChange
    =new BufferedImage(width, heigth, BufferedImage.TYPE_INT_BGR);
    Graphics2D g
    = (Graphics2D) bufferedChange.getGraphics();
    g.drawImage(bufferedImage,
    0, 0, 256, 256, null);
    g.dispose();
    bufferedChange.flush();
    ImageIO.write(bufferedChange,
    "JPEG", outFile);
    }
    catch (Exception e) {
    throw new IOException("输入文件不是允许图片!");
    }
    }
    public ImageConvertJPEG() {
    }

    public ImageConvertJPEG(String inPath, String outPath, String outFileName) {
    this.inPath = inPath;
    this.outPath = outPath;
    this.outFileName = outFileName;
    }
    }
  • 相关阅读:
    状压DP入门
    二分图匹配(最大匹配:匈牙利算法)
    序列自动机入门
    Trie树入门+例题(字典树,前缀树)
    扩展KMP算法(Z-Algorithm)
    Oracle锁表查询和解锁方法
    oracle获取系统日期--当前时间+前一天+当前月+前一个月
    oracle获取年月日,两个日期相减
    oracle decode函数和 sign函数
    expdp、impdp数据泵导出导入数据
  • 原文地址:https://www.cnblogs.com/live365wang/p/2150131.html
Copyright © 2020-2023  润新知