i am not able to read tiff images

2019-08-24 13:16发布

问题:

Here is my code. I am pasting an image over another image. This is working fine for .gif and .jpg but getting Null Pointer Exception for reading tiff images.

import java.awt.*;   
import java.awt.image.*;   
import java.io.*;     
import javax.media.jai.*; 
import javax.imageio.*;

public class Paint {   

    public static void main ( String args[] ) {   
        try {   
            // Create output file   
            OutputStream outStream = new FileOutputStream( "C:\\Users\\JavaPrg\\images\\image12.tif" );   

            // Load specified foreground and background    
            BufferedImage fgImage = ImageIO.read( new File( "C:\\Users\\JavaPrg\\images\\sign.png" ) );   
            BufferedImage bgImage = ImageIO.read( new File( "C:\\Users\\JavaPrg\\Input\\def.tif" ) );   


            BufferedImage tmpImage = new BufferedImage( fgImage.getWidth(), fgImage.getHeight(), BufferedImage.TYPE_INT_ARGB );   
            Graphics gOut = tmpImage.createGraphics();   

            // draw the foreground image on the temporary image   
            gOut.drawImage( fgImage, 0, 0, null );   
            gOut.dispose();   


            int width = tmpImage.getWidth();   
            int height = tmpImage.getHeight();   
            int[] pixels = new int[ width * height ];   
            pixels = tmpImage.getRGB( 0, 0, width, height, pixels, 0, width );   
            for ( int i = 0; i < pixels.length; i++ ) {   
                Color c = new Color( pixels[i] );   

                int r = c.getRed();   
                int g = c.getGreen();   
                int b = c.getBlue();   

                c = new Color( r, g, b);
                pixels[i] = c.getRGB();   
            }   
            tmpImage.setRGB( 0, 0, width, height, pixels, 0, width );   


            Graphics bgc = bgImage.createGraphics();   
            bgc.drawImage( tmpImage,1110 ,425 , null );   
            bgc.dispose();   

            // Save the new composite image   
            ImageIO.write( bgImage, "tif", outStream );   
            outStream.close();   
        }   
        catch ( Exception x ) {   
            x.printStackTrace();   
        }   
    }   
}  

not able to post the screenshot: it's java.lang.NullPointerException at Paint.main

标签: java jai