java intercepts picture instance code based on coordinates

  • 2020-06-15 09:09:13
  • OfStack

java intercepts the image according to the coordinates

Example code: the code is not a comment, very good to understand!


package com.json.test; 
 
  import java.awt.Rectangle;  
  import java.awt.image.BufferedImage;  
  import java.io.File;  
  import java.io.FileInputStream;  
  import java.io.IOException;  
  import java.util.Iterator;  
 
  import javax.imageio.ImageIO;  
  import javax.imageio.ImageReadParam;  
  import javax.imageio.ImageReader;  
  import javax.imageio.stream.ImageInputStream; 
 
  public class OperateImage { 
 
 
  // === Source image path names such as :c:\1.jpg   
    private String srcpath ;  
        
    // === Clipping image store path name . Such as :c:\2.jpg   
    private String subpath ;  
      
    // === The shear point x coordinates    
    private  int x ;  
      
    private  int y ;    
       
    // === Shear point width    
    private  int width ;  
      
    private  int height ;  
      
    public OperateImage() {  
          
    }    
    public OperateImage( int x, int y, int width, int height) {  
       this .x = x ;  
       this .y = y ;  
       this .width = width ;    
       this .height = height ;  
    }   
      
  /**  
    *  Crop the picture and save the new picture   .  
  */   
    public void cut()throws IOException {   
        
      FileInputStream is =  null ;  
      ImageInputStream iis = null ;  
      
      try {    
        //  Read image files    
        is =new FileInputStream(srcpath);   
          
        /*  
        *  Returns contains all currently registered  ImageReader  the  Iterator , these  ImageReader  
        *  Claims the ability to decode the specified format.   Parameters: formatName -  Contains informal format names  . 
        * (e.g.  "jpeg"  or  "tiff" ) etc.   .   
      */   
        Iterator < ImageReader > it=ImageIO.getImageReadersByFormatName("jpg");   
        ImageReader reader = it.next();   
        //  Get picture stream    
        iis = ImageIO.createImageInputStream(is);  
           
        /*  
        * <p>iis: Read the source .true: Search forward only  </p>. Mark it as   'Search only forward ' .  
        *  This setting means that images contained in the input source will be read only sequentially and may be allowed  reader 
        *  Avoid caching input portions that contain data associated with previously read images.  
      */   
        reader.setInput(iis, true ) ;  
          
        /*  
        * <p> Describes the class that how the stream is decoded <p>. Used to specify how to input from  Java Image I/O  
        *  Flow transformation in the context of the framework 1 Image or 1 The set of images. A plug-in for a specific image format  
        *  From its  ImageReader  Implementation of the  getDefaultReadParam  Return from method   
        * ImageReadParam  The instance.   
      */   
        ImageReadParam param = reader.getDefaultReadParam();   
          
        /*  
        *  Image clipping area. Rectangle  Specifies in the coordinate space 1 Three zones, through  Rectangle  object  
        *  The coordinates of the upper left vertex ( x . y ), width, and height can define this area.   
      */   
        Rectangle rect =  new Rectangle(x, y, width, height);   
          
           
        //  provide 1 a  BufferedImage , which is used as a target for decoding pixel data.    
        param.setSourceRegion(rect);   
 
        /*  
        *  Use what is provided  ImageReadParam  Read through index  imageIndex  Specifies the object and will  
        *  It is used as a 1 A complete  BufferedImage  To return.  
         */   
        BufferedImage bi=reader.read(0,param);          
       
        //  Save the new image    
        ImageIO.write(bi,"jpg",new File(subpath));     
      } finally {  
        if (is != null )  
         is.close() ;      
        if (iis != null )  
         iis.close();   
      }   
        
        
      
    }   
 
    public  int getHeight() {  
      return height;  
    }   
 
    public  void setHeight( int height) {  
      this .height = height;  
    }   
 
    public String getSrcpath() {  
      return srcpath;  
    }   
 
    public  void setSrcpath(String srcpath) {  
      this .srcpath = srcpath;  
    }   
 
    public String getSubpath() {  
      return subpath;  
    }   
 
    public  void setSubpath(String subpath) {  
      this .subpath = subpath;  
    }   
 
    public  int getWidth() {  
      return width;  
    }   
 
    public  void setWidth( int width) {  
      this .width = width;  
    }   
 
    public  int getX() {  
      return x;  
    }   
 
    public  void setX( int x)  {  
      this .x = x;  
    }   
 
    public  int getY()  {  
      return y;  
    }   
 
    public  void setY( int y) {  
      this .y = y;  
    }   
  public static void main(String[] args) { 
    OperateImage operateImage = new OperateImage(20, 20, 100, 100); 
    operateImage.srcpath = "C:/test/1.jpg"; 
    operateImage.subpath = "C:/test/2.jpg"; 
    try { 
      operateImage.cut(); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
     
  } 
} 

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: