Open png file in java

Reading/Loading an Image

When you think of digital images, you probably think of sampled image formats such as the JPEG image format used in digital photography, or GIF images commonly used on web pages. All programs that can use these images must first convert them from that external format into an internal format.

Java 2D supports loading these external image formats into its BufferedImage format using its Image I/O API which is in the javax.imageio package. Image I/O has built-in support for GIF, PNG, JPEG, BMP, and WBMP. Image I/O is also extensible so that developers or administrators can «plug-in» support for additional formats. For example, plug-ins for TIFF and JPEG 2000 are separately available.

Читайте также:  Image and description css

To load an image from a specific file use the following code, which is from LoadImageApp.java :

BufferedImage img = null; try < img = ImageIO.read(new File("strawberry.jpg")); >catch (IOException e)

Image I/O recognises the contents of the file as a JPEG format image, and decodes it into a BufferedImage which can be directly used by Java 2D.

LoadImageApp.java shows how to display this image.

If the code is running in an applet, then its just as easy to obtain the image from the applet codebase. The following excerpt is from LoadImageApplet.java :

The getCodeBase method used in this example returns the URL of the directory containing this applet when the applet is deployed on a web server. If the applet is deployed locally, getCodeBase returns null and the applet will not run.

The following example shows how to use the getCodeBase method to load the strawberry.jpg file.

Note: If you don’t see the applet running, you need to install at least the Java SE Development Kit (JDK) 7 release.

LoadImageApplet.java contains the complete code for this example and this applet requires the strawberry.jpg image file.

In addition to reading from files or URLS, Image I/O can read from other sources, such as an InputStream. ImageIO.read() is the most straightforward convenience API for most applications, but the javax.imageio.ImageIO class provides many more static methods for more advanced usages of the Image I/O API. The collection of methods on this class represent just a subset of the rich set of APIs for discovering information about the images and for controlling the image decoding (reading) process.

We will explore some of the other capabilities of Image I/O later in the Writing/Saving an Image section.

Источник

How to read and write images in Java

image formats java

In this post, I will show you how to read and write image files in Java. I will demonstrate 2 different ways of doing this. The first is with the ImageIO, and the second is with our JDeli image library.

If you are just looking for an Image Viewer, JDeli includes a built-in Image Viewer.

image formats java

How to read and write images in Java

Java provides a single type of object called a BufferedImage for images in Java.

A BufferedImage can be read from lots of different image types (ie BMP, HEIC, etc). Not all of these are supported by ImageIO itself but there are plugins to extend ImageIO, and other libraries such as Apache Imaging and JDeli.

In Java itself all the complexity of different image types is hidden – you work on a BufferedImage. Java allows direct access to the image pixels and colour information as well as allowing transformations and image processing.

Here is a list of how to read and write common Image file formats in Java.

How to read an image in Java with ImageIO

Step 1 Create a File handle, InputStream or URL pointing to the raw image.
Step 2 ImageIO will now be able to read an image file into a BufferedImage. This syntax is like so:

BufferedImage image = ImageIO.read(fileOrInputStreamOrURL)

How to read an image in Java with JDeli

Step 1 Add JDeli to your class or module path. (download the trial jar).
Step 2 Create a File, InputStream pointing to the raw image. You can also use a byte[] containing the image data.
Step 3 Read the image into a BufferedImage

BufferedImage image = JDeli.read(file);

How to write out an image in ImageIO

This example shows the exact settings for a PNG image.
Step 1 Create a File (or OutputStream) object

File file = new File("/path/to/outputFile.png"));

Step 2 Pass image, PNG type and File (or OutputStream) object into write method

ImageIO.write(bufferedImage, "PNG", file);

How to write out an image with JDeli

This example shows the exact settings for a PNG image.
Step 1 Add JDeli to your class or module path. (download link to the trial jar).

Step 2 Create a File (or OutputStream) object

File file = new File("/path/to/outputFile.png"));

Step 3 Pass image, PNG type and File (or OutputStream) object into write method

JDeli.write(bufferedImage, "PNG", file);

In JDeli you can also use a typesafe version

JDeli.write(bufferedImage, OutputFormat.PNG, file);

or pass in a PngEncoderOptions object for more control over image output.

PngEncoderOptions options = new PngEncoderOptions(); JDeli.write(bufferedImage, options, file);

Download your JDeli guide:

How to read, write and process common image file Formats in Java with JDeli

Start reading and writing images with one line of code

Read: BufferedImage image = JDeli.read(streamOrFile);

Write: JDeli.write(myBufferedImage, OutputFormat.HEIC, outputStreamOrFile)

Bethan Palmer Bethan is a Java developer and a Java Champion. She has spoken at conferences including JavaOne/Code One, DevFest and NetBeans days. She has a degree in English Literature.

Источник

How to read and write images in Java

image formats java

In this post, I will show you how to read and write image files in Java. I will demonstrate 2 different ways of doing this. The first is with the ImageIO, and the second is with our JDeli image library.

If you are just looking for an Image Viewer, JDeli includes a built-in Image Viewer.

image formats java

How to read and write images in Java

Java provides a single type of object called a BufferedImage for images in Java.

A BufferedImage can be read from lots of different image types (ie BMP, HEIC, etc). Not all of these are supported by ImageIO itself but there are plugins to extend ImageIO, and other libraries such as Apache Imaging and JDeli.

In Java itself all the complexity of different image types is hidden – you work on a BufferedImage. Java allows direct access to the image pixels and colour information as well as allowing transformations and image processing.

Here is a list of how to read and write common Image file formats in Java.

How to read an image in Java with ImageIO

Step 1 Create a File handle, InputStream or URL pointing to the raw image.
Step 2 ImageIO will now be able to read an image file into a BufferedImage. This syntax is like so:

BufferedImage image = ImageIO.read(fileOrInputStreamOrURL)

How to read an image in Java with JDeli

Step 1 Add JDeli to your class or module path. (download the trial jar).
Step 2 Create a File, InputStream pointing to the raw image. You can also use a byte[] containing the image data.
Step 3 Read the image into a BufferedImage

BufferedImage image = JDeli.read(file);

How to write out an image in ImageIO

This example shows the exact settings for a PNG image.
Step 1 Create a File (or OutputStream) object

File file = new File("/path/to/outputFile.png"));

Step 2 Pass image, PNG type and File (or OutputStream) object into write method

ImageIO.write(bufferedImage, "PNG", file);

How to write out an image with JDeli

This example shows the exact settings for a PNG image.
Step 1 Add JDeli to your class or module path. (download link to the trial jar).

Step 2 Create a File (or OutputStream) object

File file = new File("/path/to/outputFile.png"));

Step 3 Pass image, PNG type and File (or OutputStream) object into write method

JDeli.write(bufferedImage, "PNG", file);

In JDeli you can also use a typesafe version

JDeli.write(bufferedImage, OutputFormat.PNG, file);

or pass in a PngEncoderOptions object for more control over image output.

PngEncoderOptions options = new PngEncoderOptions(); JDeli.write(bufferedImage, options, file);

Download your JDeli guide:

How to read, write and process common image file Formats in Java with JDeli

Start reading and writing images with one line of code

Read: BufferedImage image = JDeli.read(streamOrFile);

Write: JDeli.write(myBufferedImage, OutputFormat.HEIC, outputStreamOrFile)

Bethan Palmer Bethan is a Java developer and a Java Champion. She has spoken at conferences including JavaOne/Code One, DevFest and NetBeans days. She has a degree in English Literature.

Источник

how to load a png-file?

Saloon Keeper

send pies

posted 14 years ago

  • Report post to moderator
  • First of all, I’m sorry if my problem has been asked many times before. I can’t find any specific questions about this. But here goes.

    I have just finished one of my very first java programs. I use the NetBeans IDE, and I stumbled on the following problem.

    My source files are in a map called «Spirograph», and I use the package «Spirograph» in my java files. One of these files needs to load a file called «blob.png», which is in the same map, for simplicity.

    I first tried to use the following code to load this png-file:

    but that didn’t work; the URL was a null pointer. The code above I picked up from some book, but I actually don’t understand a thing about class loaders.

    I then tried the following code:

    and when I compile and run this program from Netbeans it actually works! But this hard coding of the path is very unsound of course, and running the jar-file (also from Netbeans) fails (I don’t know the reason, for the dos-window that appears disappears too soon to read what’s up).

    So the question is: how can I locate the place of my package?

    There are three kinds of actuaries: those who can count, and those who can’t.

    send pies

    posted 14 years ago

  • Report post to moderator
  • The crucial question is: where is the file located? If it’s inside the jar file, then using the classloader approach is the only possible way. If it’s outside the jar file, but in the same directory, then «new File(«BLOB.PNG»)» should work. Either way, be careful about capitalization — on some operating systems «BLOB.PNG» is not the same as «blog.png».

    If you run the jar file outside of the IDE you’ll get to see any error messages. Open a command line window and type «java -jar MyJar.jar»; then any output is shown will stay visible.

    Saloon Keeper

    send pies

    posted 14 years ago

  • Report post to moderator
  • thanks for the welcome and the reply.

    First of all, how can one check what’s in a .jar file?

    The idea is indeed to run the program as a jar file. But the classloader approach still doesn’t work Running the jar from a command window, I keep getting the error of the null pointer. All java source files and the png file are in the same map «Spirograph». All java source files start with «package Spirograph». Using the approach of the File f = gives a working jar file, but of course, that makes it only working om my machine.

    So I’m still struck. Any idea where I can find more information about using packages and resource files?

    There are three kinds of actuaries: those who can count, and those who can’t.

    send pies

    posted 14 years ago

  • Report post to moderator
  • First of all, how can one check what’s in a .jar file?

    The jar tool can do that. «jar tf MyJarFile.jar» will list its contents. But unless you specifically included the image in the jar file when it got created, then it won’t be there

    All java source files and the png file are in the same map «Spirograph».

    I don’t understand what you mean by this — what is a «map» in this context? The source files are irrelevant at runtime; only the class files matter.

    Using the approach of the File f = gives a working jar file, but of course, that makes it only working om my machine.

    Have you tried removing all the directories from the path, like I suggested with «new File(«blog.png»)»? That would work on all machines. But if the point is to distribute the image file along with the class files (as part of the jar), then you’ll need to use the resource approach. Something like

    InputStream in = getClass().getResourceAsStream(«images/blob.png»);

    will look for an «images» directory relative to the current class in the jar file. If you want a top-level «images» directory, use

    InputStream in = getClass().getResourceAsStream(«/images/blob.png»);

    Источник

    Оцените статью