如何在javafx中删除图像中的空格

时间:2015-04-06 12:10:14

标签: java javafx removing-whitespace

我有一个位于屏幕顶部中央的图像,我想删除它周围的空白区域。我怎么能做到这一点?如果您需要查看我的代码,请说明。

相关代码

package whowantstobeamillionairetriviagame;

import java.io.File;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class WhoWantsToBeAMillionaireTriviaGame extends Application 
{   
@Override
public void start(Stage startingStage) throws Exception
{      
StackPane backgroundSettings = new StackPane();

Image backgroundColor = new Image("http://1.bp.blogspot.com/-p0s06MBIx_U/T8zKIBZ24pI/AAAAAAAAA7Y/n8hMZfpRic0/s1600/dark+blue+wallpaper+10.jpg");

ImageView background = new ImageView();
background.setImage(backgroundColor);

Image millionaireLogo = new Image(new File("MillionaireLogo.PNG").toURI().toString());

ImageView logoPicture = new ImageView();
logoPicture.setImage(millionaireLogo);
logoPicture.setPreserveRatio(true);
logoPicture.setSmooth(true);
logoPicture.setCache(true);
StackPane.setAlignment(logoPicture, Pos.TOP_CENTER);

backgroundSettings.getChildren().addAll(background, logoPicture);

Scene backgroundScene = new Scene(backgroundSettings);
startingStage.setScene(backgroundScene);

startingStage.setTitle("Who Wants to be a Millionaire");
startingStage.show();
}

public static void main(String[] args) 
{
    launch(args);
}
}

以下是结果Screen with an image and whitespaces

的屏幕截图

2 个答案:

答案 0 :(得分:0)

一种选择是通过Java代码调用imagemagick来修剪空白: http://www.imagemagick.org/Usage/crop/#trim

这是Java代码(前提是必须安装ImageMagick):

String trimCmd = "convert " + this.fileName + " -trim " + origFile;
try {
    Process process = Runtime.getRuntime().exec(trimCmd);
    process.waitFor();
} catch (Exception e) {
    Log.exception(e);
    return;
}

答案 1 :(得分:0)

您可以做的一件事...获取相关内容的最远,最上,最右,最下和最左点。创建一个子图像。然后将该子图像分成多个象限。创建一个双精度数组,并遍历该数组的每个部分,如果RGB颜色为[255,255,255,1],则使RGB = [255,255,255,0]。那应该消除不相关图像的空间,然后将必须存在的空白变为不需要裁剪的透明图像。我自己还没有尝试过,但是在制作业余吃豆人游戏时我做了类似的事情。下面的代码是...。如果您必须多次调用此functino,则该代码将无法执行,但是,如果像我一样只需要为整个应用程序会话创建一次图像,那么它将很好用.... < / p>

    static BufferedImage removeWhiteSpace(BufferedImage image) {
    BufferedImage wsRemoved = null;         BufferedImage wsRemoved = null;


    int width = image.getWidth();
    int height = image.getHeight();

    int furthestLeft = -1;
    int furthestRight = -1;
    int furthestUp = -1;
    int furthestDown = -1;

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            int[] pixel = image.getRaster().getPixel(x, y, (int[]) null);
            if (pixel[0] != 255 || pixel[1] != 255 || pixel[2] != 255) {
                furthestLeft = x;
                break;
            }
        }
        if (furthestLeft != -1) {
            break;
        }
    }

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int[] pixel = image.getRaster().getPixel(x, y, (int[]) null);
            if (pixel[0] != 255 || pixel[1] != 255 || pixel[2] != 255) {
                furthestUp = y;
                break;
            }
        }
        if (furthestUp != -1) {
            break;
        }
    }

    for (int y = height - 1; y > 0; y--) {
        for (int x = width - 1; x > 0; x--) {
            int[] pixel = image.getRaster().getPixel(x, y, (int[]) null);
            if (pixel[0] != 255 || pixel[1]!=255 || pixel[2] != 255) {
                furthestDown = y;
                break;
            }
        }
        if (furthestDown != -1) {
            break;
        }
    }

    for (int x = width - 1; x > 0; x--) {
        for (int y = height - 1; y > 0; y--) {
            int[] pixel = image.getRaster().getPixel(x, y, (int[]) null);
            if (pixel[0] != 255 || pixel[1] != 255 || pixel[2] != 255) {
                furthestRight = x + 1;
                break;
            }
        }
        if (furthestRight != -1) {
            break;
        }
    }

    int newWidth = furthestRight - furthestLeft;
    int newHeight = furthestDown - furthestUp;

    wsRemoved = image.getSubimage(furthestLeft, furthestUp, newWidth, newHeight);

    return wsRemoved;           return wsRemoved;
}       }