我正在更改图像中像素的LSB值,但它没有生效(Java)

时间:2013-03-02 15:38:16

标签: java rgb

我已经仔细研究过每一个问题,但是唉!所以,这是我的问题,我要做的是改变图像中RGB值的LSB。我已经使用getRGB()来获取特定像素的RGB值,将各个R,G和B值转换为8位,并且我试图将其LSB替换为来自另一个源的一点(例如文本) 。举例说明: @ pixel(0,1)R = 0111010,G = 0011101,B = 1101001,我想将它们的LSB替换为101,即新值为R = 011101(1),G = 001110(0),B = 110100 (1)。事情是在我处理它并保存我的图像后,它并没有真正保存它。这是我的代码

Scanner input = new Scanner(System.in);
    System.out.print("Enter URL: ");
    String imageURL = input.next();

    File file = new File(imageURL);
    BufferedImage image = ImageIO.read(file);

    String flag = "011111000111110011";
    int pixelX = 0;
    int pixelY = 0;

    for(int i=0; i<flag.length(); i+=3){
        int pixel = image.getRGB(pixelX, pixelY);
        int  red   = (pixel & 0x00ff0000) >> 16;
        int  green = (pixel & 0x0000ff00) >> 8;
        int  blue  =  pixel & 0x000000ff;
        System.out.println("\n@ pixel: "+pixelX+pixelY+"\nred: "+red+"green: "+green+"blue: "+blue);

        String binRed = roundTo8(Integer.toBinaryString(red));
        String binGreen = roundTo8(Integer.toBinaryString(green));
        String binBlue = roundTo8(Integer.toBinaryString(blue));
        System.out.println("\nbinRed: "+binRed+"\nbinGreen: "+binGreen+"\nbinBlue: "+binBlue);

        String flagRed = flag.substring(i, (i+1));
        String flagGreen = flag.substring((i+1), (i+2));
        String flagBlue = flag.substring((i+2), (i+3));
        System.out.println("\nflagRed: "+flagRed+"\nflagGreen: "+flagGreen+"\nflagBlue: "+flagBlue);

        if(!(flagRed.equals(binRed.charAt(7)))){
            binRed = (binRed.substring(0, 7)).concat(flagRed);
            System.out.print("\nnew binRed: "+binRed);
        }

        if(!(flagGreen.equals(binGreen.charAt(7)))){
            binGreen = (binGreen.substring(0, 7)).concat(flagGreen);
            System.out.print("\nnew binGreen: "+binGreen);
        }

        if(!(flagBlue.equals(binBlue.charAt(7)))){
            binBlue = (binBlue.substring(0, 7)).concat(flagBlue);
            System.out.print("\nnew binBlue: "+binBlue);
        }

        System.out.print("\nnew red: "+Integer.parseInt(binRed, 2));
        System.out.print("\nnew green: "+Integer.parseInt(binGreen, 2));
        System.out.print("\nnew blue: "+Integer.parseInt(binBlue, 2));
        System.out.println("==========");
        //

        int newRed = Integer.parseInt(binRed, 2);
        int newGreen = Integer.parseInt(binGreen, 2);
        int newBlue = Integer.parseInt(binBlue, 2);

        Color clr = new Color(newRed, newGreen, newBlue);
        int temp = clr.getRGB();
        image.setRGB(pixelX, pixelY, temp);

        try {
            File outputfile = new File("saved.jpg");
            ImageIO.write(image, "jpg", outputfile);
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "An error occured while saving the steg file.", "Error", JOptionPane.ERROR_MESSAGE);
        }

        pixelX++;
    }

我使用此代码测试上面的代码:

Scanner input = new Scanner(System.in);
    System.out.print("Enter URL: ");
    String imageURL = input.next();

    File file = new File(imageURL);
    BufferedImage image = ImageIO.read(file);

        for(int j=0; j<45; j++){
            int pixel = image.getRGB(j, 0);
            int  red   = (pixel & 0x00ff0000) >> 16;
            int  green = (pixel & 0x0000ff00) >> 8;
            int  blue  =  pixel & 0x000000ff;

            String binRed = roundTo8(Integer.toBinaryString(red));
            String binGreen = roundTo8(Integer.toBinaryString(green));
            String binBlue = roundTo8(Integer.toBinaryString(blue));

            System.out.print(binRed.charAt(7)+""+binGreen.charAt(7)+binBlue.charAt(7)+" ");
        }

0 个答案:

没有答案