今天的代码

/**
     * 1,图片缩放功能
     * 2,在图片文件右下角加上文件目录的名字
     *
     * @param list 文件的集合
     * @param widthdist 缩放图片的宽
     * @param heightdist 缩放图片的高
     * @return list 经过处理后的图片集合
     */



    public static List<File> reduceImg(Map<String, File> map, int widthdist, int heightdist)
    {
        List<File> listFile = new ArrayList<File>();
        if (map.size() > 0)
        {
            Set<String> set = map.keySet();
            for (Iterator<String> iterator = set.iterator(); iterator.hasNext();)
            {
                String fileName = iterator.next();
                if (!map.get(fileName).isDirectory())
                {
                    File imgTarget = null;
                    Image src = null;
                    imgTarget = new File(ConfigUtil.getWorkPath(), "data/GaoGaoMmsTemp/" + map.get(fileName).getName());
                    try
                    {
                        src = javax.imageio.ImageIO.read(map.get(fileName));
                        BufferedImage tag = new BufferedImage(widthdist, heightdist, BufferedImage.TYPE_INT_RGB);
                        tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist, Image.SCALE_SMOOTH), 0, 0, null);
                        FileOutputStream out = new FileOutputStream(imgTarget); // 把图片写到临时目录
                        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                        encoder.encode(tag);
                        out.close();
                    }
                    catch (IOException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    BufferedImage image = null;
                    try
                    {
                        image = ImageIO.read(imgTarget);

                    }
                    catch (IOException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    Graphics g = image.getGraphics();
                    g.setColor(Color.blue);
                    g.setFont(new Font("微软雅黑", Font.BOLD, 40)); // 设置字体
                    g.drawString(fileName.substring(0, 5), image.getWidth() - 115, image.getHeight()); // 设置ps上去的文字的坐标位置和文字的内容
                    try
                    {
                        ImageIO.write(image, "JPEG", imgTarget);
                    }
                    catch (IOException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    listFile.add(imgTarget);

                }
                else
                {
                    log.info("文件路径错误!");
                }
            }
        }
        else
        {
            log.info("文件参数为空!");
        }
        return listFile;
    }

    /**
     * 从文件中随机取出小于等于16张图片 此方法效率不高
     *
     * @param file 文件路径
     * @return map 中String表示文件的目录名称,File表示文件的路径
     */
    public static Map<String, File> countFile(File file)
    {
        int size = 0;
        Map<String, File> fileMap = new HashMap<String, File>();
        Map<String, File> allFileMap = new HashMap<String, File>();
        // List<File> allFile = new ArrayList<File>();
        List<String> list = new ArrayList<String>();

        if (file.exists() && file.isDirectory())
        {
            File listFile[] = file.listFiles();
            for (int i = 0; i < listFile.length; i++)
            {
                File[] fileNum = listFile[i].listFiles();
                for (int j = 0; j < fileNum.length; j++)
                {
                    if (fileNum[j].getName().indexOf(".jpg") != -1)
                    {
                        // allFile.add(fileNum[j]); // /得到所有的图片文件
                        allFileMap.put(fileNum[j].getParentFile().getName(), fileNum[j]);
                    }
                }
            }
            Set set = allFileMap.keySet();
            for (Iterator iterator = set.iterator(); iterator.hasNext();)
            {
                String object = (String) iterator.next();
                list.add(object);
            }

            size = set.size(); // 保存map的大小
            Random random = new Random(); //
            for (int i = 0; i < 16 && i < size; i++)
            {
                int j = random.nextInt(allFileMap.size()); // 构造一个随机数 范围: 0 到allFileMap.size()
                fileMap.put(list.get(j), allFileMap.get(list.get(j))); 
                allFileMap.remove(list.get(j));
                list.remove(j);
            }
        }
        Set set1 = fileMap.keySet();
        for (Iterator iterator = set1.iterator(); iterator.hasNext();)
        {
            String object = (String) iterator.next();
            System.out.println(object);
            System.out.println(fileMap.get(object));
        }
        // System.out.println(list.size());
        return fileMap;
    }

    /**
     * 把16张以内的图片组合成4*4的布局的图片
     *
     * @param list 图片的集合
     * @param width 图片组合的宽度
     * @param height图片组合的高度
     */
    public static byte[] assembleImage(List<File> list, int width, int height)
    {
        int temp = 0;
        File file = null;
        InputStream is = null;
        ByteArrayOutputStream bos = null;
        BufferedImage ImageNew = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 初始化,第一张图片在矩阵上的初始坐标
        int startX = 0;
        int startY = 0;
        int flag = 0;
        for (int i = 0; i < list.size(); i++)
        {
            BufferedImage buffer = null;
            try
            {
                // 1,对图片进行缩放
                // 2,在图片上ps上文件的名字
                buffer = ImageIO.read(list.get(i));
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            int[] imageRGB = new int[buffer.getHeight() * buffer.getWidth()];

            imageRGB = buffer.getRGB(0, 0, buffer.getWidth(), buffer.getHeight(), imageRGB, 0, buffer.getWidth());

            // 构造一个4*4的图片矩阵,并且将list(小于等于16张图片)中的所有图片加在这个图片矩阵上去
            if (flag % 2 == 0)
            {
                ImageNew.setRGB(startX, startY, buffer.getWidth(), buffer.getHeight(), imageRGB, 0, buffer.getWidth());
                startX += width / 4;
                if (startX == width)
                {
                    startX = 0;
                    startY += height / 4;
                    flag += 1;
                }

            }
            if (flag % 2 != 0)
            {
                ImageNew.setRGB(startX, startY, buffer.getWidth(), buffer.getHeight(), imageRGB, 0, buffer.getWidth());
                startX += width / 4;
                if (startX == width)
                {
                    startX = 0;
                    startY += height / 4;
                    flag += 1;
                }
            }
            if (flag == 4)
            {
                break;
            }
        }
        try
        {
            file = new File(ConfigUtil.getWorkPath(), "data/GaoGaoMmsTemp/temp/" + System.currentTimeMillis());
            ImageIO.write(ImageNew, "jpg", file);
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try
        {
            is = new FileInputStream(file); // 下面的操作是将这个组合图片转换为字节流
        }
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        bos = new ByteArrayOutputStream();
        try
        {
            while ((temp = is.read()) != -1)
            {
                bos.write(temp);
            }
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return bos.toByteArray();
    }

    /**
     * 在图片文件右下角加上图片的名字
     *
     * @param file
     */
    public void drawImage(File file)
    {
        BufferedImage image = null;
        try
        {
            image = ImageIO.read(file);

        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Graphics g = image.getGraphics();
        g.setColor(Color.blue);
        g.setFont(new Font("微软雅黑", Font.BOLD, 40)); // 设置字体
        g.drawString(file.getName().substring(0, 5), image.getWidth() - 115, image.getHeight()); // 设置ps上去的文字的坐标位置和文字的内容
        try
        {
            ImageIO.write(image, "JPEG", file);
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 多帧搞搞彩信处理
     */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值