图片合并

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URL;

/**
 * @Description 图片处理
 */
public class GraphicsUtil {
    /**
     * @Description 两张图片合并成一张图片
     * @Param [blankUrl:底版连接地址, topUrl:顶层连接地址, offsetX:x轴偏移量, offsetY:y轴偏移量, width:顶层图片宽度, height:顶层图片高度, bucket:oss根目录]
     */
    public static String merge(String blankUrl, String topUrl, int offsetX, int offsetY, int width, int height, String bucket){
        try {
            return merge(ImageIO.read(new URL(blankUrl)), ImageIO.read(new URL(topUrl)), offsetX, offsetY, width, height, bucket);
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Description 两张图片合并成一张图片
     * @Param [blankFile:底版图片, topFile:顶层图片, offsetX:x轴偏移量, offsetY:y轴偏移量, width:顶层图片宽度, height:顶层图片高度, bucket:oss根目录]
     */
    public static String merge(File blankFile, File topFile, int offsetX, int offsetY, int width, int height, String bucket){
        try {
            return merge(ImageIO.read(blankFile), ImageIO.read(topFile), offsetX, offsetY, width, height, bucket);
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Description 两张图片合并成一张图片
     * @Param [blankUrl:底版连接地址, topUrl:顶层连接地址, offsetX:x轴偏移量, offsetY:y轴偏移量, width:顶层图片宽度, height:顶层图片高度, bucket:oss根目录]
     */
    public static String merge(URL blankUrl, URL topUrl, int offsetX, int offsetY, int width, int height, String bucket){
        try {
            return merge(ImageIO.read(blankUrl), ImageIO.read(topUrl), offsetX, offsetY, width, height, bucket);
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Description 两张图片合并成一张图片
     * @Param [blankUrl:底版连接地址, topInputStream:顶层流, offsetX:x轴偏移量, offsetY:y轴偏移量, width:顶层图片宽度, height:顶层图片高度, bucket:oss根目录]
     */
    public static String merge(String blankUrl, InputStream topInputStream, int offsetX, int offsetY, int width, int height, String bucket){
        try {
            return merge(ImageIO.read(new URL(blankUrl)), ImageIO.read(topInputStream), offsetX, offsetY, width, height, bucket);
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static String merge(BufferedImage blank, BufferedImage top, int offsetX, int offsetY, int width, int height, String bucket){
        try {
            top = resize(width, height, top);
            Graphics g = blank.getGraphics();
            g.drawImage(top, blank.getWidth() - top.getWidth() - offsetX, offsetY, null);
            g.dispose();

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ImageOutputStream imageOutput = ImageIO.createImageOutputStream(byteArrayOutputStream);
            ImageIO.write(blank, "png", imageOutput);
            InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());


            //上传到oss
            //在 oss 上存放位置,文件需要带后缀,否则阿里云识别不出来
            String imagesPath = "/uploads/qrcode/temporary/" + DigestUtil.getUUID() + ".png";
            OssConstants oss = new OssConstants();
            oss.simpleUpload(imagesPath, inputStream);
            return imagesPath;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Description 对图片进行缩放
     * @Param [targetWidth:缩放后的宽度, targetHeight:缩放后的高度, image:图片]
     */
    public static BufferedImage resize(int targetWidth, int targetHeight, BufferedImage image) {
        double scaleW = (double) targetWidth / (double) image.getWidth() ;
        double scaleH = (double) targetHeight / (double) image.getHeight() ;

        double scale = scaleW < scaleH ? scaleW : scaleH;

        BufferedImage result = new BufferedImage((int) (image.getWidth() * scale), (int) (image.getHeight() * scale), BufferedImage.TYPE_INT_ARGB);

        Graphics2D g2d = result.createGraphics();
        g2d.drawImage(image, 0, 0, result.getWidth(), result.getHeight(), null);
        g2d.dispose();

        return result;
    }
}

 

你可能感兴趣的:(java,功能代码块)