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

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);
} 

}
分类:Java 时间:2010-07-22 人气:173
分享到:
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

  • 为什么 main 方法是 public static void? 2014-01-16

    0.引言: 之前在校招时,旁边的面试官问过这样一个问题:如何不在 main 函数里打印出一行字符串呢(也不允许在main里调用函数)? 如果你不能回答上来没关系,看了本文你就会有了答案.其实 main 函数我们每天 coding 都会接触,但是不一定每个同学都了解或注意到它为什么要这么设计,为什么不能那么写? 言归正传,Main方法是我们学习Java编程语言时知道的第一个方法,你是否曾经想过为什么 main 方法是 public.static.void 的?当然,很多人首先学的是C和C++,但是

  • 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

  • String与int之间的转换(java) 2012-08-18

    int -> String int i=12345; String s=""; 第一种方法:s=i+""; 第二种方法:s=String.valueOf(i); 这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢? String -> int s="12345"; int i; 第一种方法:i=Integer.parseInt(s); 第二种方法:i=Integer.valueOf(s).intValue(); 这两

  • 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

  • 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

  • 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,

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

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

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

processed in 0.858 (s). 12 q(s)