2

メソッドImageIO.read(File file);を使用してPNG画像ファイルを読み取りました。ただし、getRGB(int x, int y)メソッドを使用してアルファを抽出すると、ピクセルが透明かどうかにかかわらず、常に 255 が返されます。この不便さを解消するにはどうすればよいですか?

4

2 に答える 2

3

intパックされた色をオブジェクトに変換するときColor、アルファ値を計算する必要があるかどうかを伝える必要があります。

new Color(image.getRGB(x, y), true).getAlpha();

詳しくColor(int, boolean)はこちら

于 2013-10-18T01:37:35.187 に答える
0

メソッド getRGB(x,y) の使用は非常に非効率的であることを指摘したかっただけです。画像のピクセルを取得したい場合は、個々のピクセルから色を抽出し、そのピクセルを int 配列に格納できます。これが非効率的である理由を説明してくれた mota の功績も彼の投稿を参照してください。以下の例:

/**===============================================================================================
     * Method that extracts pixel data from an image 
     * @return a 2d array representing the pixels of the image
    =================================================================================================*/
    public static int[][] getImageData(BufferedImage img) {
            int height = img.getHeight();
            int width = img.getWidth();
            final byte[] imgPixels = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
            final boolean is_Alpha_Present = img.getAlphaRaster() != null;
            int[][] imgArr = new int[height][width];

            if (is_Alpha_Present) {
                    final int pixelLength = 4; //number of bytes used to represent a pixel if alpha value present
                    for (int pixel = 0, row = 0, col = 0; pixel < imgPixels.length; pixel = pixel + pixelLength) {
                            int argb = 0;
                            argb += (((int) imgPixels[pixel] & 0xff) << 24); //getting the alpha for the pixel
                            argb += ((int) imgPixels[pixel + 1] & 0xff); //getting the blue colour
                            argb += (((int) imgPixels[pixel + 2] & 0xff) << 8); //getting the green colour
                            argb += (((int) imgPixels[pixel + 3] & 0xff) << 16); //getting the red colour
                            imgArr[row][col] = argb;
                            col++;
                            if (col == width) {
                                    col = 0;
                                    row++;
                            }
                    }
            }
            else {
                    final int pixelLength = 3;
                    for (int pixel = 0, row = 0, col = 0; pixel < imgPixels.length; pixel = pixel + pixelLength) {
                            int argb = 0;
                            argb += Integer.MIN_VALUE;
                            argb += ((int) imgPixels[pixel] & 0xff); //getting the blue colour
                            argb += (((int) imgPixels[pixel+1] & 0xff) << 8); //getting the green colour
                            argb += (((int) imgPixels[pixel+2] & 0xff) << 16); //getting the red colour
                            imgArr[row][col] = argb;
                            col++;
                            if (col == width) {
                                    col = 0;
                                    row++;
                            }       
                    }
            }
            return imgArr;
    }
于 2013-10-18T09:47:00.170 に答える