本文主要介绍Java中,使用BufferedImage相关示例代码,以及通过ImageIO.read()获得BufferedImage对象的方法。

1、获得BufferedImage对象

File file = new File(”001.jpg“);//本地图片
BufferedImage image=(BufferedImage)ImageIO.read(file);
URL url= new URL(”http://*******“);//通过图片url
BufferedImage image=(BufferedImage)ImageIO.read(url);

2、创建BufferedImage对象

private static BufferedImage createBufferedImage(final boolean  aa) {
final BufferedImage bi = new BufferedImage(SIZE, SIZE,
BufferedImage.TYPE_INT_RGB);
final Graphics2D bg = bi.createGraphics();
bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
aa ? RenderingHints.VALUE_ANTIALIAS_ON
: RenderingHints.VALUE_ANTIALIAS_OFF);
bg.setColor(Color.RED);
bg.fillRect(0, 0, SIZE, SIZE);
bg.translate(100, 100);
bg.rotate(Math.toRadians(90));
bg.setColor(Color.BLACK);
bg.setFont(bg.getFont().deriveFont(20.0f));
bg.drawString("MMMMMMMMMMMMMMMM", 0, 0);
bg.dispose();
return bi;
}
private static BufferedImage createImage(int scale) throws Exception {
BufferedImage image = new BufferedImage(scale * 200, scale * 300,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.setColor(scale == 1 ? COLOR_1X : COLOR_2X);
g.fillRect(0, 0, scale * 200, scale * 300);
g.dispose();
return image;
}

3、获取图片的每个像素点的像素值

private static HashSet getAntialiasedColors(Object aaHint, int lcdContrast) {
        JLabel label = new JLabel("ABCD");
        label.setSize(label.getPreferredSize());
        label.putClientProperty(KEY_TEXT_ANTIALIASING, aaHint);
        label.putClientProperty(KEY_TEXT_LCD_CONTRAST, lcdContrast);
        int w = label.getWidth();
        int h = label.getHeight();
        BufferedImage buffImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = buffImage.createGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, w, h);
        label.paint(g);
        g.dispose();
        HashSet<Color> colors = new HashSet<>();
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                Color color = new Color(buffImage.getRGB(i, j));
                colors.add(color);
            }
        }
        return colors;
    }

4、压缩图片

/**
 * @param is
 * @param deistPath
 * @param newWidth
 * @param newHeight
 * @return int
 * @Title: saveCompress
 * @Description: 压缩图片,统一压缩成JPG格式,返回压缩后的大小(字节)
 */
public static int saveCompress(InputStream is, String deistPath,
                               int newWidth, int newHeight) {
    try {
        BufferedImage srcImage = ImageIO.read(is);
        BufferedImage destImage = resize(srcImage, newWidth, newHeight);
        File deistFile = new File(deistPath);
        String fileName = deistFile.getName();
        String formatName = fileName
                .substring(fileName.lastIndexOf('.') + 1);
        ImageIO.write(destImage, formatName, deistFile);
        return (int) deistFile.length();
    } catch (Exception e) {
        return -1;
    }
}

5、比较两个BufferedImage图片

/**
 * Compares two images.
 */
private static void compareImage(final BufferedImage bi1,
                                 final BufferedImage bi2)
        throws IOException {
    for (int i = 0; i < SIZE; ++i) {
        for (int j = 0; j < SIZE; ++j) {
            if (bi1.getRGB(i, j) != bi2.getRGB(i, j)) {
                ImageIO.write(bi1, "png", new File("image1.png"));
                ImageIO.write(bi2, "png", new File("image2.png"));
                throw new RuntimeException("Failed: wrong text location");
            }
        }
    }
}


推荐文档