JAVA zoom image, image cut, change colors, change the type

分类:Java 2010-07-22 来源:CodeWeblog.com 人气:172
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.Graphics;
import java.awt.color.ColorSpace;
import javax.imageio.ImageIO; 

public class ImageCut { 

/**
  *   Zoom Image
  *
  * @param srcImageFile
  *              Address of the source image file
  * @param result
  *              Address of a zoomed image
  * @param scale
  *              Scaling
  * @param flag
  *              Zoom Select  :true   Enlarge  ; false   Narrow  ;
  */
public static void scale(String srcImageFile, String result, int scale,
   boolean flag) { 

  try {
   BufferedImage src = ImageIO.read(new File(srcImageFile)); //   Read the file
   int width = src.getWidth(); //   Get source image width
   int height = src.getHeight(); //   Long been the source map
   if (flag) {
    //   Enlarge
    width = width * scale;
    height = height * scale;
   } else {
    //   Narrow
    width = width / scale;
    height = height / scale;
   }
   Image image = src.getScaledInstance(width, height,
     Image.SCALE_DEFAULT); 

   BufferedImage tag = new BufferedImage(width, height,
     BufferedImage.TYPE_INT_RGB);
   Graphics g = tag.getGraphics();
   g.drawImage(image, 0, 0, null); //   After drawing the plan reduced
   g.dispose();
   ImageIO.write(tag, "JPEG", new File(result));//   Output to the file stream   

  } catch (IOException e) {
   e.printStackTrace();
  }
} 

/**
  *   Image Cut
  *
  * @param srcImageFile
  *              Source image address
  * @param descDir
  *              Slice the destination folder
  * @param destWidth
  *              Target Chip width
  * @param destHeight
  *              Target Chip Height
  */
public static void cut(String srcImageFile, String descDir, int destWidth,
   int destHeight) {
  try {
   Image img;
   ImageFilter cropFilter;
   //   Read the source image
   BufferedImage bi = ImageIO.read(new File(srcImageFile)); 

   int srcWidth = bi.getHeight(); //   Source image width
   int srcHeight = bi.getWidth(); //   Source image height   

   if (srcWidth > destWidth && srcHeight > destHeight) { 

    Image image = bi.getScaledInstance(srcWidth, srcHeight,
      Image.SCALE_DEFAULT); 

    destWidth = 200; //   Slice width
    destHeight = 150; //   Slice height   

    int cols = 0; //   The number of horizontal slices
    int rows = 0; //   The number of vertical slices   

    //   Calculate the number of horizontal and vertical slices
    if (srcWidth % destWidth == 0) {
     cols = srcWidth / destWidth;
    } else {
     cols = (int) Math.floor(srcWidth / destWidth) + 1;
    } 

    if (srcHeight % destHeight == 0) {
     rows = srcHeight / destHeight;
    } else {
     rows = (int) Math.floor(srcHeight / destHeight) + 1;
    } 

    //   Circulation of the slice
    //   Ideas for improvement  :  Multi-threading to speed up the availability of cutting speed
    for (int i = 0; i < rows; i++) {
     for (int j = 0; j < cols; j++) {
      //  Four parameters are the starting point coordinates and width and height of image
      //   That : CropImageFilter(int x,int y,int width,int height) 

      cropFilter = new CropImageFilter(j * 200, i * 150,
        destWidth, destHeight); 

      img = Toolkit.getDefaultToolkit().createImage(
        new FilteredImageSource(image.getSource(),
          cropFilter)); 

      BufferedImage tag = new BufferedImage(destWidth,
        destHeight, BufferedImage.TYPE_INT_RGB);
      Graphics g = tag.getGraphics(); 

      g.drawImage(img, 0, 0, null); //   After drawing the plan reduced
      g.dispose();
      //   Output file
      ImageIO.write(tag, "JPEG", new File(descDir
        + "pre_map_" + i + "_" + j + ".jpg"));
     }
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
} 

/**
  *   Image type conversion
     * GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X)
     */
public static void convert(String source, String result) {
  try {
   File f = new File(source);
   f.canRead();
   f.canWrite();
   BufferedImage src = ImageIO.read(f);
   ImageIO.write(src, "JPG", new File(result));
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
} 

/**
  *   Color to black and white
  *
  * @param source
  * @param result
  */
public static void gray(String source, String result) {
  try {
   BufferedImage src = ImageIO.read(new File(source));
   ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
   ColorConvertOp op = new ColorConvertOp(cs, null);
   src = op.filter(src, null);
   ImageIO.write(src, "JPEG", new File(result));
  } catch (IOException e) {
   e.printStackTrace();
  }
} 

/**
  * @param args
  */
public static void main(String[] args) {
  //cut("C:/1.jpg", "C:/test/", 200, 150);
} 

}
分享到:
blog comments powered by Disqus

相关文章

  • public static void main (String args []) parameters 2010-05-15

    String args [] is the main function of main parameters, namely array of strings. Using eclipse in the editing, right-click to run the Java files in the Run As option, select Run Configurations. Main class in the Project and complete the project, resp ...

  • Why public static void main (String args []){} 2010-11-06

    main () is a Java program entry, program execution is the beginning of this entry. Class is a class member, main () method must be public members. So that it can be called in the execution environment. main () method does not produce the object can b

  • Producer and consumer issues to achieve product category Java: public class Product (private int id; priv 2010-05-27

    Producer and consumer issues Java implementation Product category: public class Product ( private int id; private String value; public Product (int id, String value) ( this.id = id; this.value = value; ) public int getId () ( return id; ) public void ...

  • java data type conversion (change) 2010-09-01

    Transfer from http://hi.baidu.com/loveland/blog/item/eb2d96456e64d13a8694737b.html 1 How to convert integer string String int? A. There are two ways: 1). Int i = Integer.parseInt ([String]); or i = Integer.parseInt ([String], [int radix]); 2). Int i = Int

  • java image cutting, scaling, conversion type, etc. (change) 2010-03-29

    import java.io. *; import java.awt .*; import java.awt.image .*; import java.awt.Graphics; import java.awt.color.ColorSpace; import javax.imageio.ImageIO; public class ImageCut ( / ** * Zoom the image * * @ Param srcImageFile * The source image file addre

  • Usage of Java in static.this.super.final 2010-06-25

    1, Static Please read the following this procedure: public class Hello ( public static void main (String [] args) (/ / (1) System.out.println ("Hello, world!"); / / (2) ) ) Seen this process, for most studied Java From, is not unfamiliar. If not

  • Reply: Java access to generic type 2010-03-29

    Switched from Java to obtain the generic type of response, content of fine-tuning. Java generics have such a law: Located at the side of the statement, the source where what is written to the run-time will be able to see that; In the use of one side, ...

  • Java generates RSA asymmetric public key and private key encryption (using the java API) 2010-07-16

    Non-symmetric encryption is ideal for multiple client and server secret communications between the client will use the same public key encryption which, while the public key can not reverse the decryption, the ciphertext is sent to the server with server-

  • JAVA classical algorithm of 50 questions (change) 2010-09-09

    Procedure 1】 【Title: Classical problem: a pair of rabbits, from the first 3 months after the birth of every month from birth to a pair of rabbit, bunny long to four months gave birth to a pair of rabbits every month, if the rabbit not dead, and asked the

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.574 (s). 14 q(s)