Java Utililty Methods BufferedImage Operation

List of utility methods to do BufferedImage Operation

Description

The list of methods to do BufferedImage Operation are organized into topic(s).

Method

intnrChannels(BufferedImage img)
nr Channels
switch (img.getType()) {
case BufferedImage.TYPE_3BYTE_BGR:
    return 3;
case BufferedImage.TYPE_4BYTE_ABGR:
    return 4;
case BufferedImage.TYPE_BYTE_GRAY:
    return 1;
case BufferedImage.TYPE_INT_BGR:
...
intnumPixelsDifferent(BufferedImage imgA, BufferedImage imgB)
num Pixels Different
int height = Math.min(imgA.getHeight(), imgB.getHeight());
int width = Math.min(imgA.getWidth(), imgB.getWidth());
int n = imgA.getWidth() * imgA.getHeight() + imgB.getWidth() * imgB.getHeight() - 2 * width * height;
for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
        if (imgA.getRGB(x, y) != imgB.getRGB(x, y)) {
            n += 1;
return n;
BufferedImageoffset(final BufferedImage image, final float[] scales, final float[] offsets)
Performs a rescale operation on the image's color components.
return new RescaleOp(scales, offsets, null).filter(image, null);
voidopen(BufferedImage image)
Writes image to disk and opens in default editor
File file = File.createTempFile("gscrot", ".png");
ImageIO.write(image, "png", file);
Desktop.getDesktop().open(file);
BufferedImageopenAsBufferedImage(String path)
Open as buffered image.
BufferedImage I = null;
try {
    I = ImageIO.read(new File(path));
} catch (IOException e) {
    e.printStackTrace();
return I;
BufferedImageoperatedImage(BufferedImage source, BufferedImageOp op)
Applies a BufferedImageOp on the given BufferedImage .
BufferedImage newImage = newArgbBufferedImage(source.getWidth(), source.getHeight());
Graphics2D g = (Graphics2D) newImage.getGraphics();
g.drawImage(source, op, 0, 0);
return newImage;
BufferedImageoptimizeForGraphicsHardware(BufferedImage image)
Converts an image to an optimized version for the default screen.
The optimized image should draw faster.
try {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gd.getDefaultConfiguration();
    int w = image.getWidth();
    int h = image.getHeight();
    BufferedImage optimized = gc.createCompatibleImage(w, h);
    optimized.getGraphics().drawImage(image, 0, 0, null);
...
BufferedImageoptimizeForGraphicsHardwareIfRequired(BufferedImage image)
Converts an image to an optimized version for the default screen.
The optimized image should draw faster.
try {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gd.getDefaultConfiguration();
    if (image.getColorModel().equals(gc.getColorModel())) {
        return image;
    int w = image.getWidth();
...
BufferedImageoptimizeImage(BufferedImage image)
Optimize an image or create a copy of an image if the image was created by getBufferedImage()
GraphicsConfiguration gfx_config = GraphicsEnvironment.getLocalGraphicsEnvironment()
        .getDefaultScreenDevice().getDefaultConfiguration();
if (image.getColorModel().equals(gfx_config.getColorModel())) {
    return image;
BufferedImage new_image = gfx_config.createCompatibleImage(image.getWidth(), image.getHeight(),
        image.getTransparency());
Graphics2D g2d = (Graphics2D) new_image.getGraphics();
...
intotsuTreshold(BufferedImage original)
otsu Treshold
int[] histogram = imageHistogram(original);
int total = original.getHeight() * original.getWidth();
float sum = 0;
for (int i = 0; i < 256; i++) {
    sum += i * histogram[i];
float sumB = 0;
int wB = 0;
...