Load image from a filepath via BufferedImage

前端 未结 5 1080
耶瑟儿~
耶瑟儿~ 2020-12-20 12:01

I have a problem with Java application, particular in loading a image from a location in my computer.

Following this post I used a BufferedImage and a <

相关标签:
5条回答
  • 2020-12-20 12:32

    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.

    0 讨论(0)
  • 2020-12-20 12:34

    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();
    }
    
    0 讨论(0)
  • 2020-12-20 12:39

    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

    0 讨论(0)
  • 2020-12-20 12:50
    //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();
    }
    
    0 讨论(0)
  • 2020-12-20 12:51

    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.

    0 讨论(0)
提交回复
热议问题