Java Digital Image processing basics using ImageIO to write image files sample _java

Source: Internet
Author: User
Tags sin

A bufferedimage pixel data stored in the raster, ColorModel store color space, type and other information, the current Java support only three kinds of image formats-jpg,png,gif, how to let Java support other formats, first of all to Complete the Java image read and write interface, and then hit the jar, plus startup Parameters-Xbootclasspath/pnewimageformatio.jar can.

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

Copy Code code 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:

Copy Code code 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:

Copy Code code 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 not 32-bit int type data, directly read and write RGB value can be, otherwise need from raster
object is read in the.

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

Copy Code code 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);

Read the image may be because of the larger image file, it will take a certain amount of time to wait, Java Advance image
The Processor API provides a Mediatracker object for tracking image loading, synchronizing other operations, using the following methods:
Mediatracker tracker = new Mediatracker (this); Initializing objects
Tracker.addimage (image_01, 1); Add the BufferedImage object to be tracked image_001
Tracker.waitforid (1, 10000)//wait 10 seconds for iamge_01 image to load
The code to read the RGB color value of an image from a 32-bit int data Cargb is as follows:
1 int alpha = (Cargb >>) & 0xFF; Transparency Channel
2 int red = (Cargb >>) &0xff;
3 int green = (Cargb >> 8) &0xff;
4 int blue = CARGB & 0xFF;
The code to write an RGB color value to an int 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:

Copy Code code 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 Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.