使用Java合并图片、修改DPI

项目中有时候需要对图片进行DPI、合并、拼接等的处理:

package com.snow.web.a_test;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class Test {
    
    private static String path = "F:\\";
    
    public static void main(String[] args) throws Exception {
        System.out.println("=======================================");
//        String path = "F:\\test.jpg";  
//      File files = new File(path);  
//      handleDpi(files, 300, 300); 

//        String path = "F:\\";  
        File file1 = new File(path, "1.jpg");  
        File file2 = new File(path, "2.png");  
//        mosaicImage(file1, file2); 
        mergeImage(file1, file2); 
    }
    /** 
     * 改变图片DPI 
     * 
     * @param file 
     * @param xDensity 
     * @param yDensity 
     * @throws ImageReadException 
     */  
    public static void handleDpi(File file, int xDensity, int yDensity){  
        try {  
            BufferedImage image = ImageIO.read(file);  
            JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(new FileOutputStream(file));  
            JPEGEncodeParam jpegEncodeParam = jpegEncoder.getDefaultJPEGEncodeParam(image);  
            jpegEncodeParam.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH);  
            jpegEncoder.setJPEGEncodeParam(jpegEncodeParam);  
            jpegEncodeParam.setQuality(0.75f, false);  
            jpegEncodeParam.setXDensity(xDensity);  
            jpegEncodeParam.setYDensity(yDensity);  
            jpegEncoder.encode(image, jpegEncodeParam);  
            image.flush();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    } 
    /**
     * 图片拼接
     * @param file1 左边
     * @param file2    右边
     * @throws IOException
     */
    public static void mosaicImage(File file1, File file2) throws IOException {        
        BufferedImage image1 = ImageIO.read(file1);  
        BufferedImage image2 = ImageIO.read(file2);  
  
        BufferedImage combined = new BufferedImage(image1.getWidth() * 2, image1.getHeight(), BufferedImage.TYPE_INT_RGB);  
  
        // paint both images, preserving the alpha channels  
        Graphics g = combined.getGraphics();  
        g.drawImage(image1, 0, 0, null);  
        g.drawImage(image2, image1.getWidth(), 0, null);  
          
        // Save as new image  
        ImageIO.write(combined, "JPG", new File(path, "3.jpg"));  
    }  
    /**
     * 图片合并
     * @param file1 左边
     * @param file2    右边
     * @throws IOException
     */
    public static void mergeImage(File file1, File file2) throws IOException {        
        BufferedImage image1 = ImageIO.read(file1);  
        BufferedImage image2 = ImageIO.read(file2);  
  
        BufferedImage combined = new BufferedImage(image1.getWidth(), image1.getHeight(), BufferedImage.TYPE_INT_RGB);  
  
        // paint both images, preserving the alpha channels  
        Graphics g = combined.getGraphics();  
        System.out.println(image2.getData());
        g.drawImage(image1, 0, 0, null);  
        g.drawImage(image2, 0, 0, image1.getWidth(), image1.getHeight(), null);  
          
        // Save as new image  
        ImageIO.write(combined, "JPG", new File(path, "4.jpg"));  
    }  
    
}

 

posted @ 2018-03-12 03:55  龙昊雪  阅读(835)  评论(0编辑  收藏  举报