Java 在图像中嵌入和提取数据
我写了一个在图像中嵌入和提取文本的代码。尽管我尽了最大的努力,但我还是无法理解代码中的错误,因为代码中的错误导致提取不成功。正在寻求相同方面的帮助。 这是密码Java 在图像中嵌入和提取数据,java,rgb,bufferedimage,steganography,Java,Rgb,Bufferedimage,Steganography,我写了一个在图像中嵌入和提取文本的代码。尽管我尽了最大的努力,但我还是无法理解代码中的错误,因为代码中的错误导致提取不成功。正在寻求相同方面的帮助。 这是密码 public class EmbedAndExtract { //Embedding sequence BufferedImage embed(BufferedImage img, String s) { int pixel[] = img.getRGB(0, 0, img.getWidth(), img
public class EmbedAndExtract {
//Embedding sequence
BufferedImage embed(BufferedImage img, String s) {
int pixel[] = img.getRGB(0, 0, img.getWidth(), img.getHeight(), null, 0, img.getWidth());
//the manipulated pixels will be saved in newpixel[]
int[] newpixel = Arrays.copyOf(pixel, pixel.length);
int len = s.length();
int q = 0;
//each string to be embedded will be prefixed by 4 chars which contain length of string
// for a string say "abc" s1="0003abc"
//here first four chars show length of string
for (int i = 0; i < 4; i++) {
if (len % 10 == 0) {
break;
} else {
len = len / 10;
q++;
}
}
String s1 = "";
for (int i = 0; i < 4 - q; i++) {
s1 += 0;
}
s1 += s.length();
s1 += s;
// convert s1 to byte array
byte[] b = s1.getBytes();
for (int i = 0; i < b.length; i++) {
System.out.println("" + (char) b[i]);
}
//change value of newpixel[] as per the string
int count = 0;
for (int i = 0; i < b.length; i++) {
byte current_byte = b[i];
for (int j = 7; j >= 0; j--) {
int lsb = (current_byte >> j) & 1;
newpixel[count] = (pixel[count] & 0xfffffffe) + lsb;
System.out.println(lsb + ":(" + pixel[count] + "," + newpixel[count] + ")");
count++;
}
}
//add the newpixel[] to a bufferedimage and return
BufferedImage nimg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
nimg.setRGB(0, 0, nimg.getWidth(), nimg.getHeight(), newpixel, 0, nimg.getWidth());
return nimg;
}
//Extraction sequence
String extract(BufferedImage img) {
//read pixel of image
int newpixel[] = img.getRGB(0, 0, img.getWidth(), img.getHeight(), null, 0, img.getWidth());
//extract length from first 32 pixel values i.e it will constitute 4 bytes
String qw = "";
for (int i = 0; i < 32; i++) {
qw += (newpixel[i] & 0x00000001);
}
//convert length to integer and save it in total
int n1, n2, n3, n4, total;
n1 = Integer.parseInt(qw.substring(0, 8), 2) - 48;
n2 = Integer.parseInt(qw.substring(8, 16), 2) - 48;
n3 = Integer.parseInt(qw.substring(16, 24), 2) - 48;
n4 = Integer.parseInt(qw.substring(24, 32), 2) - 48;
total = n1 * 1000 + n2 * 100 + n3 * 10 + n4;
System.out.println("" + total);
//extract string from image and reurn string
String secret = "";
int bit = 0;
for (int i = 0; i < 4 + total; i++) {
int ascii = 0;
for (int j = 7; j >= 0; j--) {
ascii += (newpixel[bit] & 1) << j;
bit++;
}
secret += (char) ascii;
}
return secret;
}
}
公共类嵌入和提取{
//嵌入序列
BuffereImage嵌入(BuffereImage img,字符串s){
int pixel[]=img.getRGB(0,0,img.getWidth(),img.getHeight(),null,0,img.getWidth());
//操纵的像素将保存在newpixel[]
int[]newpixel=Arrays.copyOf(pixel,pixel.length);
int len=s.length();
int q=0;
//每个要嵌入的字符串将以包含字符串长度的4个字符作为前缀
//对于字符串,请说“abc”s1=“0003abc”
//这里前四个字符表示字符串的长度
对于(int i=0;i<4;i++){
如果(长度%10==0){
打破
}否则{
len=len/10;
q++;
}
}
字符串s1=“”;
对于(int i=0;i<4-q;i++){
s1+=0;
}
s1+=s.长度();
s1+=s;
//将s1转换为字节数组
字节[]b=s1.getBytes();
for(int i=0;i=0;j--){
int lsb=(当前字节>>j)&1;
新像素[计数]=(像素[计数]&0xfffffffe)+lsb;
System.out.println(lsb+“:(“+pixel[count]+”,“+newpixel[count]+”);
计数++;
}
}
//将newpixel[]添加到BuffereImage并返回
buffereImage nimg=新的buffereImage(img.getWidth(),img.getHeight(),buffereImage.TYPE_4BYTE_ABGR);
nimg.setRGB(0,0,nimg.getWidth(),nimg.getHeight(),newpixel,0,nimg.getWidth());
返回nimg;
}
//提取顺序
字符串提取(BuffereImage img){
//读取图像像素
int newpixel[]=img.getRGB(0,0,img.getWidth(),img.getHeight(),null,0,img.getWidth());
//从前32个像素值中提取长度,即它将构成4个字节
字符串qw=“”;
对于(int i=0;i<32;i++){
qw+=(新像素[i]&0x00000001);
}
//将长度转换为整数并将其总计保存
内部n1、n2、n3、n4,总计;
n1=整数.parseInt(qw.子串(0,8),2)-48;
n2=整数.parseInt(qw.子串(8,16),2)-48;
n3=整数.parseInt(qw.子串(16,24),2)-48;
n4=整数.parseInt(qw.子串(24,32),2)-48;
总计=n1*1000+n2*100+n3*10+n4;
系统输出打印项次(“+总计);
//从图像中提取字符串并重新生成字符串
字符串secret=“”;
int位=0;
对于(int i=0;i<4+总计;i++){
int ascii=0;
对于(int j=7;j>=0;j--){
ascii+=(新像素[位]&1)你有没有可能在嵌入和提取之间对图像做些什么?如果你在代码中添加一个main
方法,使其成为一个完全可运行的示例,我很肯定你会得到更好的响应。haraldK在这里赚钱。如果你在嵌入秘密后立即提取秘密,你的方法就可以很好地工作。但我怀疑你有什么问题在再次加载隐写图像进行提取之前,请将其保存到jpg。普通像素替换隐写术不适用于jpg图像。除非您添加更多代码,否则我们无法复制您声称存在的任何问题,这使其成为一个离题问题。