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

如何在javafx中删除图像中的空白,java,javafx,removing-whitespace,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.imag

我有一个图像,是放在屏幕的顶部中心,我想删除它周围的空白。我怎样才能做到这一点?如果你需要看我的代码,请说出来

相关代码

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

下面是结果的屏幕截图

一个选项是通过java代码调用imagemagick来修剪空白:

以下是java代码(先决条件是安装ImageMagick):


你可以做的一件事是……获取最远、最上面、最右边、最下面和最左边的相关内容。创建一个子映像。然后将子图像分割成四个象限。制作一个双数组,循环遍历该数组的每个部分,如果RGB颜色为[255255,1],则使RGB=[255255,0]。这将消除图像中不相关的空间,然后将必须存在的空白变为透明,而不会不必要地裁剪图像。我自己也没试过,但我在做一个爱好吃豆人游戏时也做过类似的事情。代码如下。。。。如果你不得不经常调用这个函数,那么代码就没有性能,但是如果你只需要像我一样为整个应用程序会话创建一次映像,那么它就非常好用

    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;
}       }
静态BuffereImage移除空白(BuffereImage图像){
BuffereImage wsRemoved=null;BuffereImage wsRemoved=null;
int width=image.getWidth();
int height=image.getHeight();
int furthestLeft=-1;
int furthestRight=-1;
int furthestUp=-1;
int-furthestDown=-1;
对于(int x=0;x0;y--){
对于(int x=宽度-1;x>0;x--){
int[]pixel=image.getRaster().getPixel(x,y,(int[])null);
如果(像素[0]!=255 | |像素[1]!=255 | | |像素[2]!=255){
furthestDown=y;
打破
}
}
if(furthestDown!=-1){
打破
}
}
对于(int x=宽度-1;x>0;x--){
对于(int y=高度-1;y>0;y--){
int[]pixel=image.getRaster().getPixel(x,y,(int[])null);
如果(像素[0]!=255 | |像素[1]!=255 | | |像素[2]!=255){
右=x+1;
打破
}
}
如果(最右边!=-1){
打破
}
}
int newWidth=furthestRight-furthestLeft;
int newHeight=furthestDown-furthestUp;
wsRemoved=image.getSubimage(furthestLeft、furthestUp、newWidth、newHeight);
返回wsRemoved;返回wsRemoved;
}       }

请添加与问题相关的代码。欢迎使用SO。将代码添加到问题中是一种很好的做法。:)除了代码外,屏幕截图也适合这个问题。好的,谢谢你的建议,我将添加相关代码并添加屏幕截图。好的,我已经用相关数据更新了我的主要帖子。
    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;
}       }