使用Java裁剪/修剪具有空白空间的JPG文件

时间:2011-09-12 09:14:29

标签: java image crop

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

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

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

2 个答案:

答案 0 :(得分:9)

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

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"));
    }
}

答案 1 :(得分:7)

对于Android用户,以下是使用Mike Kwan答案的示例:

    public static Bitmap TrimImage(Bitmap bmp) {
    int imgHeight = bmp.getHeight();
    int imgWidth  = bmp.getWidth();

    //TRIM WIDTH
    int widthStart  = imgWidth;
    int widthEnd = 0;
    for(int i = 0; i < imgHeight; i++) {
        for(int j = imgWidth - 1; j >= 0; j--) {
            if(bmp.getPixel(j, i) != Color.TRANSPARENT &&
                    j < widthStart) {
                widthStart = j;
            }
            if(bmp.getPixel(j, i) != Color.TRANSPARENT &&
                    j > widthEnd) {
                widthEnd = j;
                break;
            }
        }
    }
    //TRIM HEIGHT
    int heightStart = imgHeight;
    int heightEnd = 0;
    for(int i = 0; i < imgWidth; i++) {
        for(int j = imgHeight - 1; j >= 0; j--) {
            if(bmp.getPixel(i, j) != Color.TRANSPARENT &&
                    j < heightStart) {
                heightStart = j;
            }
            if(bmp.getPixel(i, j) != Color.TRANSPARENT &&
                    j > heightEnd) {
                heightEnd = j;
                break;
            }
        }
    }

    int finalWidth = widthEnd - widthStart;
    int finalHeight = heightEnd - heightStart;

    return Bitmap.createBitmap(bmp, widthStart,heightStart,finalWidth, finalHeight);
}

希望这有助于某人:)

编辑:

伙计们,我刚刚更新了我的答案,因为最后一段代码只是修剪图片的结尾,而不是开头。这个工作很棒:)