-1
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();
        }
           }
    }
KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52
James
  • 1

1 Answers1

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
  • Any other tips / help? – James Aug 23 '16 at 08:57
  • 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