图片切割

package com.komlin.modular.test;

import com.komlin.component.utils.ImageCutterUtil;
import com.komlin.component.utils.ImageTools;
import com.komlin.component.utils.SpringContextUtils;
import com.komlin.modular.device.model.service.DeviceService;
import com.komlin.modular.work.model.entity.TbTraceRecord;
import com.komlin.modular.work.model.service.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.*;

/**
 * Created by Administrator on 2017/8/7.
 */
public class JavaTest{

    public static void main(String[] args) throws InterruptedException {
        String sourcePath = "E:\\apache-tomcat-7.0.78\\webapps\\QRcode\\file\\draw\\gift\\668b3d33548347388e526f0b234a27d8.jpg";
        try {
            File file = new File(sourcePath);

            BufferedImage image = ImageIO.read(file);

            // 起始坐标,剪切大小
            int x = 0;
            int y = 0;

            double destWidth = image.getWidth();
            double destHeight = image.getHeight();
            System.out.println(destWidth + " " + destHeight);
            //剪切大小
            int width = (int) Math.round(destWidth/2);
            int height = (int)Math.round(destHeight/2);

            if (width < 1 || height < 1) {
                throw new Exception("图片过小,不可切割!");
            }

            for (int i = 0;i < 2; i++) {
                y = 0;
                for (int j = 0; j < 2; j++) {
                    String sourcePath2 = "E:\\apache-tomcat-7.0.78\\webapps\\QRcode\\file\\draw\\gift\\" + i + j + ".jpg";
                    File destFile = new File(sourcePath2);
                    ImageCutterUtil.cutImage(file, destFile, x, y, width, height);
                    y += height;
                    System.out.println(x + " " + y);
                    System.out.println(width + " " + height);
                    if (destHeight - y + height < 1e-7) {
                        y = (int)destHeight - height;
                    }
                }
                x += width;
                if (destWidth - x + width < 1e-7) {
                    x = (int)destWidth - width;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}




  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值