public BufferedImage wandleBild( Image i )
{
BufferedImage bi = new BufferedImage( i.getWidth(null), i.getHeight(null), BufferedImage.TYPE_INT_RGB );
bi.getGraphics().drawImage(i,0,0,null);
return bi;
}
import java.awt.image.*;
import java.io.*;
import javax.media.jai.*;
import javax.swing.*;
import com.sun.media.jai.codec.*;
public class TifConverter
{
public static void main(String[] args) throws Exception
{
BufferedImage bi = holeSeite("c:/test.tif",0);
JFrame f = new JFrame();
f.add(new JButton(new ImageIcon(bi)));
f.pack();
f.setVisible(true);
}
public static BufferedImage holeSeite( String dateiname, int seite ) throws Exception
{
SeekableStream s = new FileSeekableStream( new File(dateiname) );
ImageDecoder d = ImageCodec.createImageDecoder("tiff", s, null);
System.out.println("Anzahl Seiten: "+ d.getNumPages());
RenderedImage op = new NullOpImage(d
.decodeAsRenderedImage(seite), null, OpImage.OP_IO_BOUND,
null);
BufferedImage bi = new BufferedImage(op.getWidth(),op.getHeight(),BufferedImage.TYPE_INT_RGB);
op.copyData(bi.getRaster());
return bi;
}
}
import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.media.jai.NullOpImage;
import javax.media.jai.OpImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.SeekableStream;
public class Test extends JFrame {
/**
* serialVersionUID
*/
private static final long serialVersionUID = 2036333296087078136L;
public Test() throws IOException {
super("TIFF Example");
super.setBounds(0, 0, 500, 500);
BufferedImage bimg = getImage(1);
JLabel jl = new JLabel();
jl.setIcon(new ImageIcon(bimg));
super.add(jl, BorderLayout.CENTER);
super.setVisible(true);
}
public BufferedImage getImage(int number) throws IOException {
// unique identifier for temp file
String uniqueIdentifier = String.valueOf(System.currentTimeMillis());
// complete filename
String fileName = "tmp-image-" + uniqueIdentifier;
// create a temp file
File tempFile = File.createTempFile(fileName, ".tmp");
// set deleteOnExit(), if an exception occures
tempFile.deleteOnExit();
SeekableStream s = new FileSeekableStream(new File("blub.tif"));
ImageDecoder d = ImageCodec.createImageDecoder("tiff", s, null);
System.out.println("Anzahl Seiten: " + d.getNumPages());
RenderedImage op = new NullOpImage(d.decodeAsRenderedImage(number),
null, OpImage.OP_IO_BOUND, null);
// write image
ImageIO.write(op, "jpg", tempFile);
// read image
BufferedImage buffImg = ImageIO.read(tempFile);
// delete tempfile
tempFile.delete();
return buffImg;
}
public static void main(String[] args) throws IOException {
new Test();
}
}
ImageDecoder d = ImageCodec.createImageDecoder("tiff", s, null);
System.out.println("Anzahl Seiten: " + d.getNumPages());
RenderedImage op = new NullOpImage(d.decodeAsRenderedImage(number),
null, OpImage.OP_IO_BOUND, null);
String dateiname = "C:\\uid0500301.tif";
SeekableStream s = new FileSeekableStream( new File(dateiname) );
ImageDecoder d = ImageCodec.createImageDecoder("tiff", s, null);
System.out.println("Anzahl Seiten: "+ d.getNumPages());
ImageIO.write(d.decodeAsRenderedImage(0),"jpg",new File("C:\\test_out.jpg"));
ImageIO.write(d.decodeAsRenderedImage(1),"jpg",new File("C:\\test_out2.jpg"));
String dateiname = "C:\\uid0500301.tif";
SeekableStream s = new FileSeekableStream( new File(dateiname) );
ImageDecoder d = ImageCodec.createImageDecoder("tiff", s, null);
System.out.println("Anzahl Seiten: "+ d.getNumPages());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ImageIO.write(d.decodeAsRenderedImage(0),"png",stream);
Image img = Toolkit.getDefaultToolkit().createImage(stream.toByteArray());
JFrame testFrame = new JFrame();
testFrame.getContentPane().add(new JLabel(new ImageIcon(img)));
testFrame.pack();
testFrame.setLocationRelativeTo(null);
testFrame.setVisible(true);
String dateiname = "C:\\uid0500301.tif";
SeekableStream s = new FileSeekableStream( new File(dateiname) );
ImageDecoder d = ImageCodec.createImageDecoder("tiff", s, null);
System.out.println("Anzahl Seiten: "+ d.getNumPages());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ImageIO.write(d.decodeAsRenderedImage(0),"png",stream);
ByteArrayInputStream in = new ByteArrayInputStream(stream.toByteArray());
BufferedImage bimg = ImageIO.read(in);