Load image from a filepath via BufferedImage

这一生的挚爱 提交于 2019-11-29 13:21:52

getResource & getResourceAsStream do not work with file paths, but paths relative the code base. If the code base is C: then a relative path that would locate the resource is /ImageTest/pic2.jpg.

..difference between load file by FileInputStream and getResourceAsStream?

One major difference is that the getResource.. will work with a resource inside a Jar, which is no longer a File. Therefore FileInputStream cannot be used to access such a resource.

To read an .jpg file from non-relative path you could use this:

BufferedImage img = null;

try 
{
    img = ImageIO.read(new File("C:/ImageTest/pic2.jpg")); // eventually C:\\ImageTest\\pic2.jpg
} 
catch (IOException e) 
{
    e.printStackTrace();
}

I do not have any Java environment at the moment, so hope it works and is written correctly.

Keerthivasan

You cannot use Class#getResource(String) or Class#getResourceAsStream(String) in this case. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream(java.lang.String).

Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:

If the name begins with a / (\u002f), then the absolute name of the resource is the portion of the name following the /. Otherwise, the absolute name is of the following form: modified_package_name/name

Where the modified_package_name is the package name of this object with / substituted for . (\u002e).

Generally, it is not a good thing to hard code the system location of your resources in your code. The neat and clean way is to put your resources in your classpath and access them. Hope this clarifies why it's not working

Harjinder Banga

To find the image Width, height and size

BufferedImage image = null;    
int imageWidth = -1;
int imageHeight = -1;
int fileSize = -1;
try {
    File imageFile = new File(imagePath);
    int fileSize = (int) imageFile.length();
    image = ImageIO.read(imageFile); // Reading the Image from the file system
    imageWidth = image.getWidth();
    imageHeight = image.getHeight();
} catch (IOException e) {
    e.printStackTrace();
}
Charles
//This code snippet read an image from location on the computer and writes it to a different location on the disk
try {
    byte[] imageInByte;

    BufferedImage imageOnDisk = ImageIO.read(new File("C:\\ImageTest\\pic2.jpg"));

    //Create a ByteArrayOutputStrea object to write image to
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    //Write the image to the OutputStream
    ImageIO.write(imageOnDisk, "jpg", baos);

    baos.flush();

    //Initialise the byte array object with the image that was written to the OutputStream
    imageInByte = baos.toByteArray();
    baos.close();

    // convert byte array back to BufferedImage
    InputStream in = new ByteArrayInputStream(imageInByte);
    BufferedImage bImageFromConvert = ImageIO.read(in);

    //write the image to a new location with a different file name(optionally)
    ImageIO.write(bImageFromConvert, "jpg", new File(
            "c:\\index.jpg"));
} catch (IOException e) {
    e.printStackTrace();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!