簡體   English   中英

更改圖像 Java 中 colors 的不透明度

[英]Changing Opacity of colors in an image Java

我編寫了一個從圖像中獲取特定 RGB 顏色並將其存儲在文件中的代碼。

現在我正在嘗試更改圖像中每個 colors 的不透明度。 例如,逐漸減少紅色直到它不存在,並且每次顏色減少時,發生的更改將保存在文件中。 怎么可能呢?

這是我為獲得特定顏色而編寫的代碼

import java.awt.image.BufferedImage;

import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import java.nio.Buffer;

import javax.imageio.ImageIO;

public class BMPtoArray {
public static void getBlue(BufferedImage image){}
public static void getGreen(BufferedImage image){}
public static void getRed(BufferedImage image){
    int blue = 0x000000FF;
    int green = 0x0000FF00;
    ColorModel cm = image.getColorModel();
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    WritableRaster raster = 
image.copyData(image.getRaster().createCompatibleWritableRaster());
    BufferedImage newImage = new BufferedImage(cm, raster, isAlphaPremultiplied, null);
    for (int row = 0; row < newImage.getHeight(); row++) {
        for (int col = 0; col < newImage.getWidth(); col++) {
            int color = newImage.getRGB(col, row);
            color &= ~green;
            color &= ~blue;
            newImage.setRGB(col, row, color);
        }
    }
    try {
        ImageIO.write(newImage, "bmp",new    File("C:\\Users\\Mhamd\\Desktop\\3rd year 
first semester\\Analysis and Coding\\labs\\2.2Java\\src\\newIMGRed"+".bmp") );
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
public static void getBlack(BufferedImage image){
    int red = 0x00FF0000;
    int blue = 0x000000FF;
    int green = 0x0000FF00;
    ColorModel cm = image.getColorModel();
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    WritableRaster raster = 
image.copyData(image.getRaster().createCompatibleWritableRaster());
    BufferedImage newImage = new BufferedImage(cm, raster, 
isAlphaPremultiplied, null);
    for(int count = 1; count<=8;count++) {
        try {
            ImageIO.write(newImage, "bmp", new 
File("C:\\Users\\Mhamd\\Desktop\\3rd year first semester\\Analysis 
and Coding\\labs\\2.2Java\\src\\newIMGBlack" + count + ".bmp"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (int row = 0; row < newImage.getHeight(); row++) {
            for (int col = 0; col < newImage.getWidth(); col++) {
                int color = newImage.getRGB(col, row);
                color &= ~blue * count / 8;
                color &= ~red * count / 8;
                color &= ~green * count / 8;
                newImage.setRGB(col, row, color);
            }
        }
    }
}
public static void main(String[] args) throws Exception {
    BufferedImage image = ImageIO.read(new File("C:\\Users\\Mhamd\\Desktop\\3rd year 
first semester\\Analysis and Coding\\labs\\2.2Java\\src\\circleRGB.bmp"));
   getRed(image);
   getBlue(image);
   getGreen(image);
}
}

getBluegetGreen方法與getRed相同,只是 colors 不同。

下圖顯示了我想要做的事情。 可以看到,圖像從它的colors開始,逐漸紅、綠、藍逐漸減少直到消失,每次顏色減少,圖像的變化都會保存在一個文件中。

圖片

逐步執行您的方法的邏輯:

for(int count = 1; count<=8;count++) {
    try {
        ImageIO.write(newImage, "bmp", new File("C:\\Users\\Mhamd\\Desktop\\3rd year first semester\\Analysis and Coding\\labs\\2.2Java\\src\\newIMGBlack" + count + ".bmp"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

在進行更改之前寫入圖像不會產生預期的結果。 您需要在修改像素寫入圖像。 刪除 Image.write 調用,並刪除 try/catch。

    for (int row = 0; row < newImage.getHeight(); row++) {
        for (int col = 0; col < newImage.getWidth(); col++) {
            int color = newImage.getRGB(col, row);

不要從newImage讀取像素。 原始圖像中讀取它:

            int color = image.getRGB(col, row);

最后,這些行是不正確的:

            color &= ~blue * count / 8;
            color &= ~red * count / 8;
            color &= ~green * count / 8;

~blue * count / 8正在對所有像素中的所有藍色值的位掩碼執行數學運算。 它沒有對實際像素值執行數學運算。

首先,隔離藍色值:

int blueValue = color & blue;

現在您有了一個可以執行數學運算的值:

blueValue = blueValue * count / 8;
blueValue &= blue;

您可以通過首先清除現有的藍色值來更新顏色:

color &= ~blue;

…然后用新的藍色值更新顏色:

color |= blueValue;

當然,你會想要對紅色和綠色值做同樣的事情。

最后,在兩個 for 循環都完成並且所有像素都已更新之后,您應該編寫文件:

            newImage.setRGB(col, row, color);
        }
    }
}

try {
    ImageIO.write(newImage, "bmp", new File("C:\\Users\\Mhamd\\Desktop\\3rd year first semester\\Analysis and Coding\\labs\\2.2Java\\src\\newIMGBlack" + count + ".bmp"));
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM