我撰寫了一個代碼,從影像中獲取特定的 RGB 顏色并將其存盤在檔案中。
現在我正在嘗試更改影像中每種顏色的不透明度。例如,逐漸減少紅色直到它不存在,并且每次顏色減少時,發生的變化都會保存在一個檔案中。那怎么辦呢?
這是我為獲得特定顏色而撰寫的代碼
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);
}
}
該getBlue和getGreen方法是一樣的getRed,只是不同的顏色。
下圖顯示了我想要做的事情。如您所見,影像從它的顏色開始,然后逐漸減少紅色、綠色和藍色直至消失,并且每次顏色減少時,影像中的變化都會保存在一個檔案中。

uj5u.com熱心網友回復:
逐步執行您的方法的邏輯:
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();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/372480.html
上一篇:組合框的網格選擇彈出視窗
下一篇:將陣列轉換為影像Java
