Some basic operations for image processing using Java

  • 2020-04-01 04:14:59
  • OfStack

An image is a set of pixels, stored in binary form. The Java language supports three major image file formats: GIF, JPEG, and BMP. The Image processing capabilities of the Java language are encapsulated in the Image class.
Image loading and output

In Java programs, images are also objects, so when an Image is loaded, the Image object is declared and then associated with the Image file using the getImage() method. There are two ways to load an image file:
Image getImage(URL URL), which indicates the location of the Image and the file name.
Image getImage(URL URL,String name), the URL indicates the location of the Image, and the name is the file name.

For example, the following code declares an Image object and associates the getImage() object with an Image file:


  Image img = getImage(getCodeBase(), " family.jpg " );


The URL(uniform Resource Location) object is used to identify the name and address of a Resource and is used when a WWW client accesses a Resource on the Internet. There are two methods to determine the position of an image: absolute position and relative position. The method of taking the relative position is as follows:
The URL getCodeBase() takes the location of the applet file.
The URL getDocumentBase() takes the location of the HTML file.

For example, the code:


  URL picURLA = new URL(getDocumentBase(), " imageSample1.gif " ),
    picURLB = new URL(getDocumentBase(), " pictures/imageSample.gif " );
  Image imageA = getImage(picURLA),imageB = getImage(picURLB);

Methods to obtain image information (properties) are as follows:
GetWidth (ImageObserver observer) : take the width;
GetHeight (ImageObserver observer) : height.

The code for the output image is written in the paint() method, and there are four ways to display the image:


boolean drawImage(Image img,int x,int y,ImageObserver observer)
boolean drawImage(Image img,int x,int y,Color bgcolor,ImageObserver observer)
boolean drawImage(Image img,int x,int y,int width,int height,ImageObsever observer)
boolean drawImage(Image img,int x,int y,int width,int height,Color bgcolor,ImageObsever observer)


Parameter img is the Image object, x,y is the top left corner of the drawing Image rectangle, the observer is the Image viewer when the Image is loaded, the bgcolor is the background color used to display the Image, and the width and height are the rectangle area to display the Image. When the area is different from the size of the Image, the display Image will be scaled.

The Applet class also implements the ImageObserver interface, which is often used as an argument. See the following code and comments:
(1) g.d rawImage (image1, 0, 0, this); // as shown in the original drawing
(2) g.d rawImage (image2, 10, 10, Color red, this); // graphics with background color display
Note: if the size of the original is different from the given range, the system will scale automatically
(3) g.d rawImage (labImag, 0, 0, this); // as shown in the original drawing
(4) g.g rawImage (labImag, 0120100100, this); // zoom display
(5) g.g rawImage (labImag, 0240500100, this); // zoom display

Applets download (get) images with init() or start() methods and display the resulting images with paint() methods.


import java.applet.*;import java.awt.*;
public class Example7_5 extends Applet{
  Image myImag;
  public void start(){
    myImag = getImage(getCodeBase(), " myPic.jpg " );
  }
  public void paint(Graphics g){
    g.drawImage(myImg,2,2,this);
  }
}

Since the getImage() method is not provided in the Frame, JFrame, and JPanel classes, they load the image using the Toolkit abstract class in the java.awt.toolkit, which has methods to load the image file:

Image.getimage (String name) : loads the Image file with the specified file name. Image.getimage (URL URL) : the uniform resource locator loads the Image file.

In this way, various components can get the Toolkit object with the getToolkit() method, and then display the image with the Toolkit object in the component's paint() method. The following code illustrates this usage:


  Toolkit tool = getToolkit();
  URL url = new URL(http://www.weixueyuan.net/image.gif);
  Image img = tool.getImage(url);


The component can also use the Toolkit's static method getDefaultToolkit() to get a default Toolkit object and load the image with it. At this point, the code to load the image is often written like this:


  Image img = Toolkit.getDefaultToolkit().getImage(url);


Related articles: