2013-03-21 79 views
0
public static void sample(BufferedImage image) { 
    int width = image.getWidth(); 
    int height = image.getHeight(); 
    int value[][] = new int[width][height]; 
    int valueR[][] = new int[width][height]; 
    int valueG[][] = new int[width][height]; 
    int valueB[][] = new int[width][height]; 
    for (int j = 0; j < height; j++) { 
     for (int i = 0; i < width; i++) { 
      int pixel = image.getRGB(i, j); 
      value[i][j] = pixel; 
      Color c = new Color(pixel); 
      valueR[i][j]= c.getRed(); 
      valueG[i][j] = c.getGreen(); 
      valueB[i][j] = c.getBlue(); 
      System.out.println("Red value = "+valueR[i][j]); 
      System.out.println("Green value ="+valueG[i][j]); 
      System.out.println("Blue value"+valueB[i][j]); 
     } 
    } 
} 

上面的代碼是將圖像的RGB值和像素顏色值分別存儲在數組中。查找像素位置

public static BigInteger modPow(BigInteger a1, BigInteger e, BigInteger n) { 

    BigInteger r = 1; 


    for (int i = e.bitLength() - 1; i >= 0; i--) { 
     r = (r.multiply(r)).mod(n); 
     if (e.testBit(i)) { 
      r = (r.multiply(a1)).mod(n); 
     } 
    } 
    System.out.println("C value = " + r); 

    int lng = 3; 
    BigInteger bi = BigInteger.valueOf(lng); 
    BigInteger a = r.divide(bi); 
    BigInteger b = r.mod(bi); 
    System.out.println("pixel position a = " + a); 
    System.out.println("modulus value b = " + b); 
    return r; 
} 

在上面的代碼正在尋找像素位置,其中i需要嵌入祕密bit.so我需要去那個特定像素嵌入message.But在前面的代碼正在存儲像素顏色在數組值[] []。我需要搜索數組值[] []來獲取我在上一代碼中得到的像素位置。

注: A1是信息文件的當前位的嵌入 位置{E,N}是公鑰

我的問題是如何找到的像素位置?

+0

我假設你的意思是'a是當前位信息...'而不是'a1',但即使如此你的問題還不清楚。 「像素位置」是什麼意思?你是否設想了一個編號系統,其中左上角的像素編號爲1,而右下方的像素編號爲「width * height」或類似的東西? – ach 2013-03-21 17:09:17

回答

1

查找像素的位置是一個複雜執行的簡單概念。我在這裏寫了一些代碼,需要一個BufferedImage,並通過它搜索一個特定顏色的像素

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

public class pixelSearch { 

    public static void main(String[] args) { 
     //I don't know where you're getting your image but i'll get one from file 
     File image = new File("image.bmp"); 
     try { 
      BufferedImage imageToSearch = ImageIO.read(image); 

      Color colorToFind = new Color(255,255,255); //define color to search for with RGB vals 255,255,255 
      //for more information on constructing colors look here: http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html 

      int[] pixelCoordinates = pSearch(colorToFind, imageToSearch); //search for the pixel 
      System.out.println("Found pixel at (" + pixelCoordinates[0] + "," + pixelCoordinates[1] + ")."); //display coordinates 
     } catch (IOException e) { 
      System.out.println(e.toString()); 
     } 
    } 

    private static int[] pSearch (Color c, BufferedImage pic){     
     int cVal = c.getRGB(); //get integer value of color we are trying to find 

     int x1 = 0; 
     int y1 = 0; 
     int x2 = pic.getWidth(); 
     int y2 = pic.getHeight(); 

     int[] XArray = new int[x2-x1+1]; //create an array to hold all X coordinates in image 
     int iterator = 0; 
     while (iterator <= x2) { 
      XArray[iterator] = x1 + iterator; 
      iterator++; 
     } 
     int [] YArray = new int[y2-y1+1]; //create an array to hold all Y coordinates in image 
     iterator = 0; 
     while (iterator <= y2) { 
      YArray[iterator] = y1 + iterator; 
      iterator++; 
     } 

     //next we iterate throug all the possible coordinates to check each pixel 
     for (int yVal : YArray) { 
      for (int xVal : XArray) { 
       int color = pic.getRGB(xVal, yVal); //get the color of pixel at coords (xVal, yVal) 
       if (color == cVal) { //if the color is equal to the one we inputted to the function 
        int[] cPos = {xVal, yVal}; //store the coordinates 
        return cPos; //return the coordinates 
       } 
      } 
     } 

     int[] returnVal = {-1,-1}; //if we didn't find it return -1, -1 
     return returnVal; 
    } 
}