Java圖片處理方法——放大、縮小、切割

來源:互聯網
上載者:User

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(); // 源圖高度
*/           
            int srcWidth = bi.getWidth(); // 源圖寬度
            int srcHeight = bi.getHeight(); // 源圖高度
           
            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 * destWidth, i * destHeight, 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("c://test//456.jpg","C://test//image1.jpg",2,false);
        cut("pictrue/pic_"+1+".jpg","C://test//image2.jpg",100,100);
//        gray("c://test//456.jpg","C://test//image4.jpg");
      
        System.out.println("end");
    }

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.