Calling image 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.

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.

Читайте также:  Python script module name

Источник

Lesson: Working with Images

As you have already learned from the Images lesson, Image s are described by a width and a height, measured in pixels, and have a coordinate system that is independent of the drawing surface.

There are a number of common tasks when working with images.

  • Loading an external GIF, PNG JPEG image format file into the internal image representation used by Java 2D.
  • Directly creating a Java 2D image and rendering to it.
  • Drawing the contents of a Java 2D image on to a drawing surface.
  • Saving the contents of a Java 2D image to an external GIF, PNG, or JPEG image file.

This lesson teaches you the basics of loading, displaying, and saving images.

The are two main classes that you must learn about to work with images:

  • The java.awt.Image class is the superclass that represents graphical images as rectangular arrays of pixels.
  • The java.awt.image.BufferedImage class, which extends the Image class to allow the application to operate directly with image data (for example, retrieving or setting up the pixel color). Applications can directly construct instances of this class.

The BufferedImage class is a cornerstone of the Java 2D immediate-mode imaging API. It manages the image in memory and provides methods for storing, interpreting, and obtaining pixel data. Since BufferedImage is a subclass of Image it can be rendered by the Graphics and Graphics2D methods that accept an Image parameter.

A BufferedImage is essentially an Image with an accessible data buffer. It is therefore more efficient to work directly with BufferedImage . A BufferedImage has a ColorModel and a Raster of image data. The ColorModel provides a color interpretation of the image’s pixel data.

The Raster performs the following functions:

  • Represents the rectangular coordinates of the image
  • Maintains image data in memory
  • Provides a mechanism for creating multiple subimages from a single image data buffer
  • Provides methods for accessing specific pixels within the image

The basic operations with images are represented in the following sections:

Reading/Loading an image

This section explains how to load an image from an external image format into a Java application using the Image I/O API

Drawing an image

This section teaches how to display images using the drawImage method of the Graphics and Graphics2D classes.

Creating and drawing To an image

This section describes how to create an image and how to use the image itself as a drawing surface.

Writing/saving an image

This section explains how to save created images in an appropriate format.

Источник

Class Image

The abstract class Image is the superclass of all classes that represent graphical images. The image must be obtained in a platform-specific manner.

Field Summary

Choose an image-scaling algorithm that gives higher priority to scaling speed than smoothness of the scaled image.

Читайте также:  Python checking exception type

The UndefinedProperty object should be returned whenever a property which was not defined for a particular image is fetched.

Constructor Summary

Method Summary

Returns an ImageCapabilities object which can be inquired as to the capabilities of this Image on the specified GraphicsConfiguration.

Methods declared in class java.lang.Object

Field Details

accelerationPriority

Priority for accelerating this image. Subclasses are free to set different default priorities and applications are free to set the priority for specific images via the setAccelerationPriority(float) method.

UndefinedProperty

The UndefinedProperty object should be returned whenever a property which was not defined for a particular image is fetched.

SCALE_DEFAULT

SCALE_FAST

Choose an image-scaling algorithm that gives higher priority to scaling speed than smoothness of the scaled image.

SCALE_SMOOTH

SCALE_REPLICATE

Use the image scaling algorithm embodied in the ReplicateScaleFilter class. The Image object is free to substitute a different filter that performs the same algorithm yet integrates more efficiently into the imaging infrastructure supplied by the toolkit.

SCALE_AREA_AVERAGING

Use the Area Averaging image scaling algorithm. The image object is free to substitute a different filter that performs the same algorithm yet integrates more efficiently into the image infrastructure supplied by the toolkit.

Constructor Details

Image

Method Details

getWidth

Determines the width of the image. If the width is not yet known, this method returns -1 and the specified ImageObserver object is notified later.

getHeight

Determines the height of the image. If the height is not yet known, this method returns -1 and the specified ImageObserver object is notified later.

getSource

Gets the object that produces the pixels for the image. This method is called by the image filtering classes and by methods that perform image conversion and scaling.

getGraphics

Creates a graphics context for drawing to an off-screen image. This method can only be called for off-screen images.

getProperty

Gets a property of this image by name. Individual property names are defined by the various image formats. If a property is not defined for a particular image, this method returns the UndefinedProperty object. If the properties for this image are not yet known, this method returns null , and the ImageObserver object is notified later. The property name «comment» should be used to store an optional comment which can be presented to the application as a description of the image, its source, or its author.

getScaledInstance

Creates a scaled version of this image. A new Image object is returned which will render the image at the specified width and height by default. The new Image object may be loaded asynchronously even if the original source image has already been loaded completely. If either width or height is a negative number then a value is substituted to maintain the aspect ratio of the original image dimensions. If both width and height are negative, then the original image dimensions are used.

Читайте также:  Вывести все простые делители числа питон

flush

  • BufferedImage objects leave the primary Raster which stores their pixels untouched, but flush any information cached about those pixels such as copies uploaded to the display hardware for accelerated blits.
  • Image objects created by the Component methods which take a width and height leave their primary buffer of pixels untouched, but have all cached information released much like is done for BufferedImage objects.
  • VolatileImage objects release all of their pixel resources including their primary copy which is typically stored on the display hardware where resources are scarce. These objects can later be restored using their validate method.
  • Image objects created by the Toolkit and Component classes which are loaded from files, URLs or produced by an ImageProducer are unloaded and all local resources are released. These objects can later be reloaded from their original source as needed when they are rendered, just as when they were first created.

getCapabilities

Returns an ImageCapabilities object which can be inquired as to the capabilities of this Image on the specified GraphicsConfiguration. This allows programmers to find out more runtime information on the specific Image object that they have created. For example, the user might create a BufferedImage but the system may have no video memory left for creating an image of that size on the given GraphicsConfiguration, so although the object may be acceleratable in general, it does not have that capability on this GraphicsConfiguration.

setAccelerationPriority

Sets a hint for this image about how important acceleration is. This priority hint is used to compare to the priorities of other Image objects when determining how to use scarce acceleration resources such as video memory. When and if it is possible to accelerate this Image, if there are not enough resources available to provide that acceleration but enough can be freed up by de-accelerating some other image of lower priority, then that other Image may be de-accelerated in deference to this one. Images that have the same priority take up resources on a first-come, first-served basis.

getAccelerationPriority

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

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