i am not able to read tiff images

懵懂的女人 提交于 2019-12-12 00:58:29

问题


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

来源:https://stackoverflow.com/questions/25051253/i-am-not-able-to-read-tiff-images

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!