使用Java裁剪/修剪具有空白空间的JPG文件 [英] Crop/trim a JPG file with empty space with Java

查看:1261
本文介绍了使用Java裁剪/修剪具有空白空间的JPG文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有框架能够移除图像的白色空间(矩形)。我们从技术图纸创建图像缩略图,遗憾的是PDF格式。我们将PDF转换为SVG,然后转换为JPG。通常技术图纸非常小,现在位于缩略图的左上角:

Is there a framework which is able to remove the white space (rectangular) of an image. We create Image Thumbnails from technical drawings which are unfortunately in PDF format. We convert the PDF to SVG and then to JPG. Often the technical drawings are very small and now placed in the upper left corner of the thumbnail:

+---------+----------------------+
|         |                      |
| (image) |                      |
|         |                      |
+---------+                      |
|                                |
|                                |
|                                |
|                                |
|              (empty space)     |
|                                |
|                                |
+--------------------------------+

那么如何轻松删除空格并缩小JPG文件?

So how can I easily remove the empty space and shrink the JPG file?

推荐答案

可以在JAI中完成,如此主题所示。或者这里是我刚刚编写的一些Java代码,可用于执行此操作:

It can be done in JAI as is demonstrated in this thread. Or here's some Java code I just wrote which can be used to do it:

public class TrimWhite {
    private BufferedImage img;

    public TrimWhite(File input) {
        try {
            img = ImageIO.read(input);
        } catch (IOException e) {
            throw new RuntimeException( "Problem reading image", e );
        }
    }

    public void trim() {
        int width  = getTrimmedWidth();
        int height = getTrimmedHeight();

        BufferedImage newImg = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics g = newImg.createGraphics();
        g.drawImage( img, 0, 0, null );
        img = newImg;
    }

    public void write(File f) {
        try {
            ImageIO.write(img, "bmp", f);
        } catch (IOException e) {
            throw new RuntimeException( "Problem writing image", e );
        }
    }

    private int getTrimmedWidth() {
        int height       = this.img.getHeight();
        int width        = this.img.getWidth();
        int trimmedWidth = 0;

        for(int i = 0; i < height; i++) {
            for(int j = width - 1; j >= 0; j--) {
                if(img.getRGB(j, i) != Color.WHITE.getRGB() &&
                        j > trimmedWidth) {
                    trimmedWidth = j;
                    break;
                }
            }
        }

        return trimmedWidth;
    }

    private int getTrimmedHeight() {
        int width         = this.img.getWidth();
        int height        = this.img.getHeight();
        int trimmedHeight = 0;

        for(int i = 0; i < width; i++) {
            for(int j = height - 1; j >= 0; j--) {
                if(img.getRGB(i, j) != Color.WHITE.getRGB() &&
                        j > trimmedHeight) {
                    trimmedHeight = j;
                    break;
                }
            }
        }

        return trimmedHeight;
    }

    public static void main(String[] args) {
        TrimWhite trim = new TrimWhite(new File("...\\someInput.bmp"));
        trim.trim();
        trim.write(new File("...\\someOutput.bmp"));
    }
}

这篇关于使用Java裁剪/修剪具有空白空间的JPG文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆