I have written many articles on image processing, but I have not introduced the Java 2D image processing API, which is discussed and mentioned in this article.
The APIs are based on JDK 6. First, let's look at how Java organizes an image object BufferedImage,
Pixel data of a BufferedImage is stored in Raster, and color space and type are stored in ColorModel.
Information. Currently, Java only supports three image formats-JPG, PNG, and GIF.
Complete the image read/write interface in Java, compress it into a jar, and add the startup parameter-Xbootclasspath/p.
NewimageformatIO. jar.
In Java, you can use an ImageIO object to read and write an image file. The code for reading an image file is as follows:
File file = File("D:\\test\\blue_flower.jpg"= ImageIO.read(file);
The code for writing an image file is as follows:
File outputfile = File("saved.png""png",outputfile);
The code for reading pixel data from a BufferedImage object is as follows:
type= ( type ==BufferedImage.TYPE_INT_ARGB || type == ( image.getRGB( x, y, width, height, pixels, 0, width );
First, obtain the image type. If it is not 32-bit INT data, read and write the RGB value directly. Otherwise
Object.
Writing pixel data to a BufferedImage object also follows the preceding rules. The Code is as follows:
type= ( type ==BufferedImage.TYPE_INT_ARGB || type == image.setRGB(x, y, width, height, pixels, 0, width );
Reading an Image may take some time to read because the Image file is large. Java Advance Image
The Processor API provides a MediaTracker object to track image loading and synchronize other operations. The procedure is as follows:
MediaTracker tracker = MediaTracker(); tracker.addImage(image_01, 1); tracker.waitForID(1, 10000)
The code for reading the RGB color values of an image from a 32-bit int-type data cARGB is as follows:
alpha = (cARGB >> 24)& 0xff; red = (cARGB >> 16) &0xff green = (cARGB >> 8) &0xff blue = cARGB & 0xff;
The code for writing an RGB color value into an INT-type data cRGB is as follows:
cRGB = (alpha << 24) | (red<< 16) | (green << 8) | blue;
The code for creating a BufferedImage object is as follows:
BufferedImage image = newBufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
A complete source code Demo is as follows:
PlasmaDemo serialVersionUID = -2236160343614397287L BufferedImage image = size = 256 .setOpaque( Graphics2D g2 = g2.drawImage(getImage(), 5, 5, image.getWidth(), image.getHeight(), (image == image = [] rgbData = [size* setRGB(image, 0, 0 File outFile = File("plasma.jpg" ImageIO.write(image, "jpg" } generateNoiseImage( index = 0 a = 255 r = 0 g = 0 b = 0 ( row=0; row<size; row++ ( col=0; col<size; col++ r = ()(128.0 + (128.0 * Math.sin((row + col) / 8.0 g = ()(128.0 + (128.0 * Math.sin((row + col) / 8.0 b = ()(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++ clamp( (rgb > 255 255 (rgb < 0 0 setRGB( BufferedImage image, x, y, width, height, type = ( type == BufferedImage.TYPE_INT_ARGB || type == image.setRGB( x, y, width, height, pixels, 0 JFrame frame = JFrame("Noise Art Panel" frame.getContentPane().setLayout( frame.getContentPane().add( frame.setPreferredSize( Dimension(400 + 25,450 frame.setVisible( }