如何使用JavaFx删除图像的颜色(使背景透明)? [英] How to delete a color of an image with JavaFx (make the background transparent)?

查看:573
本文介绍了如何使用JavaFx删除图像的颜色(使背景透明)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的图片中装有图像 ImageView



我想制作图像的背景,白色,透明以匹配背景。

解决方案

使用



将TOLERANCE_THRESHOLD减小到0xCF会产生如下图像,其中一些浅灰色背景变为透明。请注意,这个结果并不完美,因为角色周围仍然有一个别名的灰色轮廓,角色的白色头盔变得透明。您可以将算法修改为更加智能,并且更像


I have an image loaded with Image and ImageView.

I want to make the background of the image, which is white, transparent to match with the background.

解决方案

See the resampling technique using a PixelWriter and WritableImage used in Reduce number of colors and get color of a single pixel. You can use the same technique and change the white pixels (rgb 255,255,255) to transparent (rgba 0,0,0,0).

You could also do this in an image editing program before you use the image in your application.

Here is a sample using the image from Easily Remove White Or Black Backgrounds with Blending Sliders in Photoshop.

A simple resampling implementation is shown below:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class XRayVision extends Application {
    private static final int TOLERANCE_THRESHOLD = 0xFF;
    private static final String BACKGROUND_IMAGE_LOC = 
            "http://imgs.abduzeedo.com/files/articles/20_beautiful_landscape_wallpapers/landscape-wallpaper-1.jpg";
    private static final String ORIGINAL_IMAGE_LOC = 
            "http://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2011/01/toy.jpg";

    private Image makeTransparent(Image inputImage) {
        int W = (int) inputImage.getWidth();
        int H = (int) inputImage.getHeight();
        WritableImage outputImage = new WritableImage(W, H);
        PixelReader reader = inputImage.getPixelReader();
        PixelWriter writer = outputImage.getPixelWriter();
        for (int y = 0; y < H; y++) {
            for (int x = 0; x < W; x++) {
                int argb = reader.getArgb(x, y);

                int r = (argb >> 16) & 0xFF;
                int g = (argb >> 8) & 0xFF;
                int b = argb & 0xFF;

                if (r >= TOLERANCE_THRESHOLD 
                        && g >= TOLERANCE_THRESHOLD 
                        && b >= TOLERANCE_THRESHOLD) {
                    argb &= 0x00FFFFFF;
                }

                writer.setArgb(x, y, argb);
            }
        }

        return outputImage;
    }

    @Override
    public void start(final Stage stage) throws Exception {
        final Image backgroundImage = new Image(BACKGROUND_IMAGE_LOC);
        final ImageView backgroundImageView = new ImageView(backgroundImage);
        final Image originalImage = new Image(ORIGINAL_IMAGE_LOC);
        final ImageView originalImageView = new ImageView(originalImage);
        final Image resampledImage = makeTransparent(originalImage);
        final ImageView resampledImageView = new ImageView(resampledImage);

        final HBox images = new HBox(originalImageView, resampledImageView);
        stage.getIcons().add(originalImage);

        final StackPane layout = new StackPane(backgroundImageView, images);
        stage.setScene(new Scene(layout));
        stage.show();
    }

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

When the TOLERANCE_THRESHOLD is set to 0xFF, only pure white pixels will be changed to transparent, and you get the image as below. The white portion around the character is because some of the background is not exactly white but is slightly gray, so the resampling doesn't modify it's alpha value.

Decreasing the TOLERANCE_THRESHOLD to 0xCF produces an image like below, where some of the light gray background becomes transparent. Note that this outcome is not perfect as it there is still an aliased gray outline around the character and the white helmet of the character becomes transparent. You could modify the algorithm to be a bit more intelligent and act more like the lasso tool of an image drawing application, however that would probably get tricky and may still make mistakes without a human involved. If the simplistic programmatic routine below outlined above is not enough to correctly resample your image as you like, you will be best off setting up the transparency ahead of time in an image editing program (which is what I would recommend for most cases in any case).

这篇关于如何使用JavaFx删除图像的颜色(使背景透明)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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