java 对透明图的处理(几个工具)

获取透明背景的坐标list

    /**
     * 获取透明背景的坐标list
     *
     * @param file
     * @return
     */
    @SneakyThrows
    public static int[][] getTransparentByUrl(File file) {
        BufferedImage bufImg = null;
        try {
            bufImg = ImageIO.read(file);
        } catch (IOException e) {
            throw new BusinessException("file地址错误" + e);
        }
        int height = bufImg.getHeight();
        int width = bufImg.getWidth();

        int[][] place = new int[width][height];

        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int pixel = bufImg.getRGB(i, j);
                if (pixel >> 24 == 0) {
                    place[i][j] = 0;
                } else {
                    place[i][j] = 1;
                }
            }
        }

        return place;
    }

将特定颜色替换为透明


    /**
     * 将特定颜色替换为透明
     * @param filePath          待处理图片地址(G:\java\BeiShi\1234\bigRBG.png
     * @param targetFile        保存目标地址(G:\java\BeiShi\1234\bigRBG.png
     * @param rbgColor          替换颜色(255,255,255)
     * @throws IOException
     */
    public static void changeImgColor(String filePath,String targetFile, int[] rbgColor) throws IOException {
        String substring = filePath.substring(filePath.lastIndexOf("."));
        Image image = ImageIO.read(new File(filePath));

        //将原图片的二进制转化为ImageIcon
        ImageIcon imageIcon = new ImageIcon(image);
        int width = imageIcon.getIconWidth();
        int height = imageIcon.getIconHeight();

        //图片缓冲流
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D graphics2D = (Graphics2D) bufferedImage.getGraphics();
        graphics2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());

        //将特定颜色替换为透明
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int rgb = bufferedImage.getRGB(i, j);

                int[] write = new int[3];
                write[0] = (rgb & 0xff0000) >> 16;
                write[1] = (rgb & 0xff00) >> 8;
                write[2] = (rgb & 0xff);
                if (write==rbgColor) {
                    rgb = (0) | (rgb & 0x00ffffff);
                    bufferedImage.setRGB(i, j, rgb);
                }
            }
        }

        ImageIO.write(bufferedImage, substring, new File(targetFile));
    }

查询图片是否为透明图


    /**
     * 查询图片是否为透明图
     *
     * @param file 文件路径G:\java\BeiShi\excel_imgs\***.png
     * @return boolean
     */
    public static boolean isTransparentByFile(File file) {
        BufferedImage bufImg = null;
        try {
            bufImg = ImageIO.read(file);
        } catch (IOException e) {
            throw new BusinessException("file地址错误" + e);
        }
        int height = bufImg.getHeight();
        int width = bufImg.getWidth();
        boolean isTransparent = false;
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int pixel = bufImg.getRGB(i, j);
                if (pixel >> 24 == 0) {
                    isTransparent = true;
                    break;
                }
            }
        }
        return isTransparent;
    }

借鉴文章:https://blog.csdn.net/qq_41940067/article/details/118609354

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OFD是一种复杂的文档格式,其中可能包含多个图层和透明度设置。当将OFD转换为图片时,可能会出现图像遮挡文字的问题,这可能是由于OFD文件中的图层顺序或透明度设置引起的。 要解决这个问题,您可以尝试以下几点: 1. 调整图层顺序 在OFD文件中,不同的图层可以通过设置不同的Z-order来控制它们的顺序。您可以尝试重新排列图层的顺序,以确保文本在图像之上。例如,您可以使用`OFDPage`类的`getContent()`方法获取页面内容,然后使用`OFDContent`类的`getElements()`方法获取所有元素,并根据它们的Z-order重新排序。例如: ``` OFDContent content = page.getContent(); List<OFDElement> elements = content.getElements(); Collections.sort(elements, new Comparator<OFDElement>() { @Override public int compare(OFDElement e1, OFDElement e2) { return e1.getZOrder() - e2.getZOrder(); } }); ``` 2. 调整透明度设置 OFD文件中的元素可以设置透明度,如果图像设置了透明度,则可能会遮挡文本。您可以尝试将所有元素的透明度设置为1,以确保文本不会被遮挡。例如: ``` for (OFDElement element : elements) { element.setAlpha(1); } ``` 3. 尝试使用不同的转换方法 如果前两个方法无法解决问题,您可以尝试使用不同的OFD转换方法,例如使用Adobe Acrobat或其他OFD转换工具。另外,您也可以尝试将OFD文件导入到其他文档编辑器中,如Microsoft Word或Adobe InDesign,并将其导出为图像格式。 希望这些方法可以帮助您解决问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值