Java digital image processing base USES imageio to write image file examples

  • 2020-04-01 02:51:54
  • OfStack

A BufferedImage pixel data stored in the Raster, ColorModel stored inside color space, the information such as type, current Java support only three kinds of image formats - JPG, PNG, GIF, how to enable Java support other formats, to complete the first interface in Java image, speaking, reading and writing, and then into the jar, and launch parameters - Xbootclasspath/pnewimageformatIO. Jar.

How to read and write an image file in Java, using the ImageIO object. The code to read the image file is as follows:

 


File file = new File("D:\test\blue_flower.jpg");
BufferedImage image = ImageIO.read(file);

The code to write the image file is as follows:


File outputfile = new File("saved.png");
ImageIO.write(bufferedImage, "png",outputfile);

The code to read pixel data from the BufferedImage object is as follows:


int type= image.getType();
if ( type ==BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
return (int [])image.getRaster().getDataElements(x, y, width, height, pixels );
else
return image.getRGB( x, y, width, height, pixels, 0, width );

First get the image type, if it is not 32-bit INT data, read and write the RGB value directly, otherwise need from Raster
Object.

Writing pixel data to a BufferedImage object also follows the above rules. The code is as follows:


int type= image.getType();
if ( type ==BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
image.getRaster().setDataElements(x, y, width, height, pixels );
else
image.setRGB(x, y, width, height, pixels, 0, width );

It may take some time to read the Image because the Image file is large. Java Advance Image
The Processor API provides the MediaTracker object to track image loading and synchronize other operations, using the following methods:
MediaTracker tracker = new MediaTracker(this); // initializes the object
The tracker. AddImage (image_01, 1); // adds the BufferedImage object image_001 that you want to trace
Tracker. waitForID(1, 10000) // wait 10 seconds for iamge_01 image to load
The code to read the image RGB color value from a 32-bit int data cARGB is as follows:
1 int alpha = (cARGB > > 24) & 0 XFF; // transparency channel
2 int red = (cARGB > > 16) & 0 XFF;
3 int green = (cARGB > > 8) & 0 XFF;
4 int blue = cARGB & 0xff;
The code to write RGB color value into an INT type data cRGB is as follows:
CRGB = (alpha < < 24) | (red< < 16) | (green < < 8) | blue;
The code to create a BufferedImage object is as follows:
BufferedImage image = newBufferedImage(256, 256, bufferedimage.type_int_argb);
A complete source code Demo is as follows:


 package com.gloomyfish.swing;

 import java.awt.BorderLayout;
 import java.awt.Dimension;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.RenderingHints;
 import java.awt.image.BufferedImage;
 import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class PlasmaDemo extends JComponent {  

      
    private static final long serialVersionUID = -2236160343614397287L;  
    private BufferedImage image = null;  
    private int size = 256;

    public PlasmaDemo() {  
        super();  
        this.setOpaque(false);  
    }  

    protected void paintComponent(Graphics g) {  
        Graphics2D g2 = (Graphics2D)g;  
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  
        g2.drawImage(getImage(), 5, 5, image.getWidth(), image.getHeight(), null);  
    }  

    private BufferedImage getImage() {  
        if(image == null) {  
            image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);  
            int[] rgbData = new int[size*size];  
            generateNoiseImage(rgbData);  
            setRGB(image, 0, 0, size, size, rgbData);
            File outFile = new File("plasma.jpg");
            try {
                ImageIO.write(image, "jpg", outFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }  
        return image;  
    }  

    public void generateNoiseImage(int[] rgbData) {  
        int index = 0;  
        int a = 255;  
        int r = 0;  
        int g = 0;  
        int b = 0;  

        for(int row=0; row<size; row++) {  
            for(int col=0; col<size; col++) {  
                // set random color value for each pixel  
                r = (int)(128.0 + (128.0 * Math.sin((row + col) / 8.0)));  
                g = (int)(128.0 + (128.0 * Math.sin((row + col) / 8.0)));  
                b = (int)(128.0 + (128.0 * Math.sin((row + col) / 8.0)));  

                rgbData[index] = ((clamp(a) & 0xff) << 24) |  
                                ((clamp(r) & 0xff) << 16)  |  
                                ((clamp(g) & 0xff) << 8)   |  
                                ((clamp(b) & 0xff));  
                index++;  
            }  
        }  

    }  

    private int clamp(int rgb) {  
        if(rgb > 255)  
            return 255;  
        if(rgb < 0)  
            return 0;  
        return rgb;  
    }    

    public void setRGB( BufferedImage image, int x, int y, int width, int height, int[] pixels ) {  
        int type = image.getType();  
        if ( type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )  
            image.getRaster().setDataElements( x, y, width, height, pixels );  
        else  
            image.setRGB( x, y, width, height, pixels, 0, width );  
    }  

    public static void main(String[] args) {  
        JFrame frame = new JFrame("Noise Art Panel");  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        frame.getContentPane().setLayout(new BorderLayout());  

        frame.getContentPane().add(new PlasmaDemo(), BorderLayout.CENTER);  
        frame.setPreferredSize(new Dimension(400 + 25,450));  
       frame.pack();  
       frame.setVisible(true);  
   }  
}  


Related articles: