0

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

Flot2011
  • 4,601
  • 3
  • 44
  • 61
user23385
  • 111
  • 2
  • 12
  • You need to install an image reader that knows how to read tiff files. Try https://github.com/haraldk/TwelveMonkeys – yshavit Jul 31 '14 at 05:44
  • I checked wht reader or writer i m having. It has everything except Tiff. This is my output of that.. reader BMP reader bmp reader jpg reader JPG reader wbmp reader jpeg reader png reader PNG reader JPEG reader WBMP reader GIF reader gif writer jpg writer BMP writer bmp writer JPG writer jpeg writer wbmp writer png writer PNG writer JPEG writer WBMP writer GIF writer gif can i mannually register .tif reader and writer in code – user23385 Jul 31 '14 at 06:18
  • is jai_imageio.jar is in class-path? – Sanjeev Jul 31 '14 at 06:28
  • yup path of jar is "C:\Program Files (x86)\Java\jdk1.7.0_55\jre\lib\ext\jai_imageio.jar" classpath is "C:\Program Files (x86)\Java\jdk1.7.0_55\jre\lib\ext;.;" – user23385 Jul 31 '14 at 06:36
  • now ImageIO jdk.exe jre.exe and lib.exe everything is installed – user23385 Jul 31 '14 at 06:38
  • The most simple solution seems to be to not write the output as a .tif file, is it possible to use .png (or something else) instead? – Mizipzor Jul 31 '14 at 08:23
  • yeah output file format is not an issue... But i am not able to read the tiff file. I checked Jars is at the Place. classpath set . I checked in Jar in has the ImageIOtiffwriterspi but not able to load.. I tried using code too public static void register(){ IIORegistry registry = IIORegistry.getDefaultInstance(); registry.registerServiceProvider(new TIFFImageWriterSpi()); registry.registerServiceProvider(new TIFFImageReaderSpi()); } but still getting error NoClassdefFound – user23385 Jul 31 '14 at 08:33
  • No Help!!! ?? :( how to load these classes – user23385 Jul 31 '14 at 09:20
  • The error is in Loading the classes from the Jar. Is there any other method to load the TiffImage Writer/Reader classes – user23385 Jul 31 '14 at 10:42
  • Did you try TwelveMonkeys? – yshavit Jul 31 '14 at 12:45
  • I am not getting how to use that exactly. Should i copy the code and create and package and then try. Please suggest. How to procced with that – user23385 Jul 31 '14 at 13:27
  • There are instructions in the repo; basically, just put the classes or jars in your classpath. If you're using maven, just adding the dependency is enough; otherwise you'll have to compile this (easiest using maven) and put that jar on your classpath. If you need help with that, it's a separate question (though I'd encourage you to try to read up on docs online first, because it's a fairly broad question that probably wouldn't do well on SO). – yshavit Jul 31 '14 at 18:22
  • i am not using any IDE my code is in notepad using cmd only. i am having time constraints. now i am focused to convert tiff to jpg format. – user23385 Aug 01 '14 at 04:15
  • Even in conversion i m getting the same error while running NoClassDefFound Error. its compiled properly while running this is the erroe what can be the issue – user23385 Aug 01 '14 at 04:23

0 Answers0