简体   繁体   English

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

[英]How can I remove white spaces in an image in javafx

I have an image that is placed at the top center of the screen and I would like to remove the white spaces around it.我有一个位于屏幕顶部中央的图像,我想删除它周围的空白区域。 How would I be able to accomplish this?我将如何能够做到这一点? If you need to see my code please say so.如果您需要查看我的代码,请说出来。

relevant code相关代码

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

Here is a screenshot of the result这是结果的屏幕截图带有图像和空格的屏幕

One option is to invoke imagemagick through java code to trim the whitespace: http://www.imagemagick.org/Usage/crop/#trim一种选择是通过java代码调用imagemagick来修剪空白: http : //www.imagemagick.org/Usage/crop/#trim

Here is java code (pre-requisite is to install ImageMagick):这是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;
}

One thing you can do...Get the furthest, top,right,bottom and left points of the content that is relevant.您可以做的一件事...获取相关内容的最远、顶部、右侧、底部和左侧点。 Create a subimage of that.创建一个子图像。 Then split that subImage up into quadrants.然后将该子图像分成象限。 Make a double array and iterate through each part of that and make the RGB = [255,255,255, 0] if the RGB color is [255,255,255,1].创建一个双数组并遍历其中的每一部分,如果 RGB 颜色为 [255,255,255,1],则使 RGB = [255,255,255, 0]。 That should eliminate space of the image that is not relevant and then turn the whitespace that has to be there with out undesirably cropping your image transparent.这应该消除不相关的图像空间,然后将必须存在的空白变为透明,而不会意外地裁剪图像。 I haven't tried this myself but I did something similar when I was making a hobby pacman game.我自己没有尝试过这个,但是当我制作一个爱好吃豆子的游戏时,我做了类似的事情。 The code is below .... The code is not performant if you have to call this functino a lot, but if you only need to create the image once for the entire application session like I do then it works great....代码在下面......如果你必须多次调用这个functino,代码就不是高性能的,但是如果你只需要像我一样为整个应用程序会话创建一次图像,那么它工作得很好......

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM