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 = new File("D:\\test\\blue_flower.jpg");BufferedImage image = ImageIO.read(file);
The code for writing an image file is as follows:
File outputfile = new File("saved.png");ImageIO.write(bufferedImage, "png",outputfile);
The code for reading pixel data from a 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, 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:
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 );
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 = new mediatracker (this); // initialize the object tracker. addimage (image_01, 1); // Add the bufferedimage object image_001tracker.waitforid (1, 10000) to be tracked. // wait for 10 seconds for the iamge_01 image to be loaded.
The code for reading the RGB color values of an image from a 32-bit int-type data cargb is as follows:
Int alpha = (cargb> 24) & 0xff; // transparency channel int Red = (cargb> 16) & 0xff; int Green = (cargb> 8) & 0xff; int 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:
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()); // Display the window. frame.getContentPane().add(new PlasmaDemo(), BorderLayout.CENTER); frame.setPreferredSize(new Dimension(400 + 25,450)); frame.pack(); frame.setVisible(true); } }