显示TIF文件
- 运行结果
package src;
import com.sun.media.jai.codec.*;
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.TIFFEncodeParam;
import com.sun.media.jai.codec.TIFFDecodeParam;
import com.sun.media.jai.codec.JPEGEncodeParam;
import java.awt.image.RenderedImage;
import javax.media.jai.RenderedOp;
import javax.media.jai.JAI;
import java.awt.*;
import java.awt.image.DataBuffer;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.*;
//本类继承自画布类,用作绘图的面板,因为Java不允许多继承,所以要用此类
class TIFBase extends Canvas
{
ImageDecoder dec;
TIFBase() throws IOException {
this.TifRead();
}
public void TifRead() throws IOException
{
String currentDir = System.getProperty("user.dir");
System.out.println("当前目录:" + currentDir);
FileSeekableStream ss = new FileSeekableStream("human_brain_from_itk_example1.tif");
//FileSeekableStream ss = new FileSeekableStream("ex_Repo_hb9_eve1.tif");
TIFFDecodeParam param0 = null;
TIFFEncodeParam param = new TIFFEncodeParam();
JPEGEncodeParam param1 = new JPEGEncodeParam();
dec = ImageCodec.createImageDecoder("tiff", ss, param0);
param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
param.setLittleEndian(false); // Intel
}
public void TifDisplay(Graphics g) throws IOException, InterruptedException
{
int count = dec.getNumPages();
System.out.println("This TIF has " + count + " image(s)");
System.out.println();
for (int i = 0; i < count; i++)
{
System.out.println("image: " + i);
RenderedImage page = dec.decodeAsRenderedImage(i);
DataBuffer dataBuffer = page.getData().getDataBuffer();
//System.out.println("size: " + dataBuffer.getSize());
int height = page.getHeight();
int width = page.getWidth();
//g.drawString(page.getData().toString(), 0, 0);
for (int j = 0; j < height; j++)
{
for (int k = 0; k < width; k++)
{
int red = dataBuffer.getElem((j * width + k) * 3);
int green = dataBuffer.getElem((j * width + k) * 3 + 1);
int blue = dataBuffer.getElem((j * width + k) * 3 + 2);
g.setColor(new Color(red, green, blue));
g.drawOval(j, k, 1, 1);
}
}
}
//Thread.sleep(10);
}
public void TifDisplay2(Graphics g) throws IOException, InterruptedException {
int pagesCount = dec.getNumPages();
System.out.println("This TIF has " + pagesCount + " image(s)");
System.out.println();
DataBuffer[] dataBuffers = new DataBuffer[pagesCount];
int height = 0;
int width = 0;
for (int i = 0; i < pagesCount; i++)
{
//System.out.println("image: " + i);
RenderedImage page = dec.decodeAsRenderedImage(i);
//System.out.println("height: " + page.getHeight() + ", width: " + page.getWidth());
height = page.getHeight();
width = page.getWidth();
dataBuffers[i] = page.getData().getDataBuffer();
}
int statPage = 0;
int endPage = pagesCount;
int gap = endPage - statPage;
int miniColor = 3;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int redSumary = 0;
int greenSumary = 0;
int blueSumary = 0;
int validRedColorFlag = 0;
int validGreenColorFlag = 0;
int validBlueColorFlag = 0;
for (int k = statPage; k < endPage; k++)
{
int red = dataBuffers[k].getElem((i * width + j) * 3);
int green = dataBuffers[k].getElem((i * width + j) * 3 + 1);
int blue = dataBuffers[k].getElem((i * width + j) * 3 + 2);
if (red > miniColor)
{
redSumary += red;
validRedColorFlag++;
}
if (green > miniColor)
{
greenSumary += green;
validGreenColorFlag++;
}
if (blue > miniColor)
{
blueSumary += blue;
validBlueColorFlag++;
}
}
redSumary = (validRedColorFlag == 0) ? 0 : (redSumary / validRedColorFlag);
greenSumary = (validGreenColorFlag == 0) ? 0 : (greenSumary / validGreenColorFlag);
blueSumary = (validBlueColorFlag == 0) ? 0 : (blueSumary / validBlueColorFlag);
g.setColor(new Color(redSumary, greenSumary, blueSumary));
g.drawOval(i, j, 1, 1);
}
}
}
public void paint(Graphics g)
{
System.out.println("1");
try {
//this.TifDisplay(g);
this.TifDisplay2(g);
}
catch (IOException | InterruptedException e)
{
throw new RuntimeException(e);
}
}
}
public class TIF extends JFrame
{
public TIF()
{
super("画直线");
this.setVisible(true);
this.setBounds(200, 200, 600, 600);
}
public void Display() throws IOException
{
//创建对象
TIFBase tifBase = new TIFBase();//创建实例
this.getContentPane().add(tifBase);
}
public static void main(String[] args) throws IOException
{
TIF tif = new TIF();
tif.Display();
}
}
运行结果