1

我已经彻底研究了每一个问题,但是,唉!所以,这是我的问题,我想做的是改变图像中 RGB 值的 LSB。我已经使用 getRGB() 来获取特定像素的 RGB 值,将单个 R、G 和 B 值转换为 8 位,我试图用另一个来源的位替换它们的 LSB(例如文本) . 举例说明:@像素 (0, 1) R=0111010, G=0011101 和 B=1101001 我想用 101 替换它们的 LSB,即新值将是 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)+" ");
        }
4

0 回答 0