zoukankan      html  css  js  c++  java
  • JAVA图片处理--缩放,切割,类型转换

    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 ChangeImageSize
    {
        /** *//**
         * 缩放图像
         * @param srcImageFile 源图像文件地址
         * @param result       缩放后的图像地址
         * @param scale        缩放比例
         * @param flag         缩放选择:true 放大; false 缩小;
         */
        public static void scale(String srcImageFile, String result, int scale, boolean flag)
        {
            try
            {
                BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件
                int width = src.getWidth(); // 得到源图宽
                int height = src.getHeight(); // 得到源图长
                if (flag)
                {
                    // 放大
                    width = width * scale;
                    height = height * scale;
                }
                else
                {
                    // 缩小
                    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); // 绘制缩小后的图
                g.dispose();
                ImageIO.write(tag, "JPEG", new File(result));// 输出到文件流
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    
        /** *//**
         * 图像切割
         * @param srcImageFile 源图像地址
         * @param descDir      切片目标文件夹
         * @param destWidth    目标切片宽度
         * @param destHeight   目标切片高度
         */
        public static void cut(String srcImageFile, String descDir, int destWidth, int destHeight)
        {
            try
            {
                Image img;
                ImageFilter cropFilter;
                // 读取源图像
                BufferedImage bi = ImageIO.read(new File(srcImageFile));
                int srcWidth = bi.getHeight(); // 源图宽度
                int srcHeight = bi.getWidth(); // 源图高度
                if (srcWidth > destWidth && srcHeight > destHeight)
                {
                    Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT);
                    destWidth = 200; // 切片宽度
                    destHeight = 150; // 切片高度
                    int cols = 0; // 切片横向数量
                    int rows = 0; // 切片纵向数量
                    // 计算切片的横向和纵向数量
                    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;
                    }
                    // 循环建立切片
                    // 改进的想法:是否可用多线程加快切割速度
                    for (int i = 0; i < rows; i++)
                    {
                        for (int j = 0; j < cols; j++)
                        {
                            // 四个参数分别为图像起点坐标和宽高
                            // 即: 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); // 绘制缩小后的图
                            g.dispose();
                            // 输出为文件
                            ImageIO.write(tag, "JPEG", new File(descDir + "pre_map_" + i + "_" + j + ".jpg"));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    
        /** *//**
         * 图像类型转换 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();
            }
        }
    
        /** *//**
         * 彩色转为黑白
         * @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)
        {
            scale("D:100CASIOCIMG0001.JPG","C:Documents and Settingsibm桌面image.jpg",10,false);
        }
    
    }

     文章转自:http://www.cnblogs.com/chinaqiao/archive/2009/06/04/1496170.html

  • 相关阅读:
    c# -- 实现浏览功能(备忘)
    自己动手写中文分词解析器完整教程,并对出现的问题进行探讨和解决(附完整c#代码和相关dll文件、txt文件下载)
    爬虫技术 -- 进阶学习(九)使用HtmlAgilityPack获取页面链接(附c#代码及插件下载)
    爬虫技术 -- 进阶学习(八)模拟简单浏览器(附c#代码)
    爬虫技术 -- 进阶学习(七)简单爬虫抓取示例(附c#代码)
    c# -- 介绍File.AppendAllText 方法
    c# -- 解决FromsAuthentication上下文不存在
    c# -- Form1_Load()不被执行的三个解决方法
    爬虫技术 -- 基础学习(六)解析相对地址
    爬虫技术 -- 基础学习(五)解决页面编码识别(附c#代码)
  • 原文地址:https://www.cnblogs.com/dreammyle/p/3992983.html
Copyright © 2011-2022 走看看