import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
    public class ImageTest {
           public static void main(String args[]){
                  ImageTest imageTest = new ImageTest();
                  imageTest.testImage();
           }
        void testImage() {
        int x = 10;
        int y = 10;
        int w = 24;
        int h = 44;
        //String path = "D:images\\upload_final\\030311175258.jpg";
        //String path = "D:\\screens\\testcd.jpg";
        String path = "D:\\Vision\\tmpsvs\\New\\20F.TIF";
        System.out.println("Path===>"+path);
        BufferedImage out = null;
        BufferedImage image = null;
        try {
            image = ImageIO.read(new File(path));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
             //out = image.getSubimage(x, y, w, h);
        try {
            out =ImageIO.read(new File(path)).getSubimage(x, y, w, h);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            ImageIO.write(out, "TIF", new File(path));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
           }
    }
            Asked
            
        
        
            Active
            
        
            Viewed 121 times
        
    -1
            
            
        - 
                    Did you try to use debugger? – ByeBye Aug 23 '16 at 07:41
- 
                    Yeah, i got the below when doing so; – James Aug 23 '16 at 07:44
- 
                    Path===>D:\Vision\tmpsvs\New\20F.TIF Exception in thread "main" java.lang.NullPointerException at ImageTest.testImage(ImageTest.java:34) at ImageTest.main(ImageTest.java:10) – James Aug 23 '16 at 07:45
1 Answers
0
            
            
        Try this - Make sure \ is escaped. On Windows, single backslash must be escaped. See the code below.
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageTest {
public static void main(String args[]) {
    ImageTest imageTest = new ImageTest();
    imageTest.testImage();
}
void testImage() {
    int x = 10;
    int y = 10;
    int w = 24;
    int h = 44;
    String path = "C:\\Users\\xxx\\yyy\\Documents\\M\\tumblr_nz324ifAzI1sgxxaao1_500.jpg";
    System.out.println("Path===>" + path);
    BufferedImage out = null;
    BufferedImage image = null;
    try {
        image = ImageIO.read(new File(path));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}
 
    
    
        Sanjeev Dhiman
        
- 1,169
- 1
- 11
- 20
- 
                    Thanks Sanjeev...I tried both \\ and //. Both produced the same. For some reason, I'm not able to read the file. I have checked the path and its in order. – James Aug 23 '16 at 07:55
- 
                    
- 
                    Got it, its just that TIF file cannot be simply crop as per to the above code. Anybody has experience in cropping a TIF image? – James Aug 23 '16 at 10:27
 
    