2016-10-04 160 views
0

編程語言:Java的將某些像素RGB顏色到另一種顏色

我想這個圖像的某些像素轉換成不同的顏色,露出了‘祕密信息’。

Original Image

大多數像素是:

紅色= 0,綠色= 64,藍色= 0

,我想改變爲R = 255中的像素,G = 255 ,B = 255:

紅色= 5,綠色= 64,藍色= 5

import java.io.File; 
import java.io.IOException; 
import java.awt.image.BufferedImage; 
import javax.imageio.ImageIO; 
import java.awt.Color; 

public class ASSN2p2 
{ 

    private static int makeRGBColor(int red, int green, int blue) 
{ 
int rgb = 0; 
rgb = red*65536 + green*256 + blue; 
return rgb; 
} 
private static int getRed(int pixel) 
{ 
return (pixel >> 16) & 0xFF; 
} 
private static int getGreen(int pixel) 
{ 
return (pixel >> 8) & 0xFF; 
} 
private static int getBlue(int pixel) 
{ 
return (pixel) & 0xFF; 
} 




    public static void main(String args[]) throws IOException 
    { 
    // int width = 300; 
    // int height = 200;  
    BufferedImage image = null; 
    File f = null; 

    try 
    { 
     f = new File("D:\\2016-2017\\Fall2016\\201_CSCE_Programming\\Assignment 2\\secretmessage.png"); 
     image = ImageIO.read(f); 
     image = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); 
     System.out.println("Reading Complete"); 
     BufferedImage output = new BufferedImage(image.getWidth(), image.getHeight(),BufferedImage.TYPE_INT_ARGB); 

     //-------------------------------------- 


     //------------------------------------------- 


for (int y = 0; y < image.getHeight(); y++) 
{ 
for (int x = 0; x < image.getWidth(); x++) 
{ 
int pixel = image.getRGB(x, y); 
int r,g,b; 
r = getRed(pixel); 
g = getGreen(pixel); 
b = getBlue(pixel); 
if ((r == 5) && (g == 64) && (b == 5)) 
{ 
r = 64; 
b = 64; 
b = 64; 
image.setRGB(x,y,makeRGBColor(r,g,b)); 
} } 
} 
    } 
    catch(IOException e) 
    { 
     System.out.println("Error: "+e); 
    } 



     // printPixelARGB(pixel); 
     // System.out.println(""); 







    try 
    { 
     f = new File("D:\\2016-2017\\Fall2016\\201_CSCE_Programming\\Assignment 2\\output.png"); 
     ImageIO.write(image, "png", f); 
     System.out.println("Writing Complete"); 
    } 
    catch(IOException e) 
    { 
     System.out.println("Error: "+e); 
    } 

    } 
} 

這會產生這樣的:

Output

我無法理解了什麼問題!

任何援助將不勝感激!

回答

0

我相信你錯過了新顏色的alpha分量。將您的代碼更改爲:

private static int makeRGBColor(int red, int green, int blue) { 
    int rgb = 0xff000000 | (red << 16) | (green << 8) | blue; 
    return rgb; 
} 

確保alpha設置爲full,否則您的圖像看起來完全透明。

+0

如何將alpha設置爲完整? – Tristan

+0

這就是'0xff000000'所做的。 –

相關問題