java处理图片
Java Advanced Imaging相关资料
API:? http://www.oracle.com/technetwork/java/jaifaq-138288.html?ssSourceSiteId=otncn#multipage
手册:http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/JAITOC.fm.html
Introduction to Java Advanced Imaging: http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Introduction.doc.html#47285
先关代码
?
import java.awt.Frame;
import java.awt.image.renderable.ParameterBlock;
import java.io.IOException;
import javax.media.jai.Interpolation;
import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import com.sun.media.jai.codec.FileSeekableStream;
import javax.media.jai.widget.ScrollingImagePanel;
/**
?* This program decodes an image file of any JAI supported
?* formats, such as GIF, JPEG, TIFF, BMP, PNM, PNG, into a
?* RenderedImage, scales the image by 2X with bilinear
?* interpolation, and then displays the result of the scale
?* operation.
?*/
public class JAISampleProgram {
?
?String fileName = "C:\\Users\\hejian.DOMRST\\Desktop\\Booklet\\20111024_112457_0001.tiff";
??? /** The main method. */
??? public static void main(String[] args) {
??????? /* Validate input. */
??????? if (args.length != 1) {
??????????? System.out.println("Usage: java JAISampleProgram " +
?????????????????????????????? "input_image_filename");
??????????? System.exit(-1);
??????? }
??????? /*
???????? * Create an input stream from the specified file name
???????? * to be used with the file decoding operator.
???????? */
??????? FileSeekableStream stream = null;
??????? try {
??????????? stream = new FileSeekableStream(args[0]);
??????? } catch (IOException e) {
??????????? e.printStackTrace();
??????????? System.exit(0);
??????? }
??????? /* Create an operator to decode the image file. */
??????? RenderedOp image1 = JAI.create("stream", stream);
??????? /*
???????? * Create a standard bilinear interpolation object to be
???????? * used with the "scale" operator.
???????? */
??????? Interpolation interp = Interpolation.getInstance(
?????????????????????????????????? Interpolation.INTERP_BILINEAR);
??????? /**
???????? * Stores the required input source and parameters in a
???????? * ParameterBlock to be sent to the operation registry,
???????? * and eventually to the "scale" operator.
???????? */
??????? ParameterBlock params = new ParameterBlock();
??????? params.addSource(image1);
??????? params.add(2.0F);???????? // x scale factor
??????? params.add(2.0F);???????? // y scale factor
??????? params.add(0.0F);???????? // x translate
??????? params.add(0.0F);???????? // y translate
??????? params.add(interp);?????? // interpolation method
??????? /* Create an operator to scale image1. */
??????? RenderedOp image2 = JAI.create("scale", params);
??????? /* Get the width and height of image2. */
??????? int width = image2.getWidth();
??????? int height = image2.getHeight();
??????? /* Attach image2 to a scrolling panel to be displayed. */
??????? ScrollingImagePanel panel = new ScrollingImagePanel(
??????????????????????????????????????? image2, width, height);
??????? /* Create a frame to contain the panel. */
??????? Frame window = new Frame("JAI Sample Program");
??????? window.add(panel);
??????? window.pack();
??????? window.show();
??? }
}
?
import java.io.File;
import java.io.IOException;
import java.awt.Frame;
import java.awt.image.RenderedImage;
import javax.media.jai.widget.ScrollingImagePanel;
import javax.media.jai.NullOpImage;
import javax.media.jai.OpImage;
import com.sun.media.jai.codec.SeekableStream;
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.TIFFDecodeParam;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.ImageCodec;
public class MultiPageRead extends Frame {
??? ScrollingImagePanel panel;
??? public MultiPageRead(String filename) throws IOException {
??????? setTitle("Multi page TIFF Reader");
??????? File file = new File(filename);
??????? SeekableStream s = new FileSeekableStream(file);
??????? TIFFDecodeParam param = null;
??????? ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
??????? System.out.println("Number of images in this TIFF: " +
?????????????????????????? dec.getNumPages());
??????? // Which of the multiple images in the TIFF file do we want to load
??????? // 0 refers to the first, 1 to the second and so on.
??????? int imageToLoad = 0;
??????? RenderedImage op =
??????????? new NullOpImage(dec.decodeAsRenderedImage(imageToLoad),
??????????????????????????? null,
??????????????????????????? OpImage.OP_IO_BOUND,
??????????????????????????? null);
??????? // Display the original in a 800x800 scrolling window
??????? panel = new ScrollingImagePanel(op, 800, 800);
??????? add(panel);
??? }
??? public static void main(String [] args) {
??????? String filename = "C:\\Users\\hejian.DOMRST\\Desktop\\Booklet\\20111213133838620.tif";
??????? try {
??????????? MultiPageRead window = new MultiPageRead(filename);
??????????? window.pack();
??????????? window.show();
??????? } catch (java.io.IOException ioe) {
??????????? System.out.println(ioe);
??????? }
??? }
}