infohiding.java
来自「使用java编写的LSB图像信息隐藏算法演示程序」· Java 代码 · 共 524 行 · 第 1/2 页
JAVA
524 行
tmpPixel=(byte) (this.originalImageData[countPixel] & 0x7f);
tmpText=(byte) ((textByte & 0x01)<<7);
this.targetImageData[countPixel]=(byte) (tmpPixel + tmpText);
countPixel++;
countPixel++;
tmpPixel=(byte) (this.originalImageData[countPixel] & 0x7f);
tmpText=(byte) ((textByte & 0x02)<<6);
this.targetImageData[countPixel]=(byte) (tmpPixel + tmpText);
countPixel++;
countPixel++;
tmpPixel=(byte) (this.originalImageData[countPixel] & 0x7f);
tmpText=(byte) ((textByte & 0x04)<<5);
this.targetImageData[countPixel]=(byte) (tmpPixel + tmpText);
countPixel++;
countPixel++;
tmpPixel=(byte) (this.originalImageData[countPixel] & 0x7f);
tmpText=(byte) ((textByte & 0x08)<<4);
this.targetImageData[countPixel]=(byte) (tmpPixel + tmpText);
countPixel++;
countPixel++;
tmpPixel=(byte) (this.originalImageData[countPixel] & 0x7f);
tmpText=(byte) ((textByte & 0x10)<<3);
this.targetImageData[countPixel]=(byte) (tmpPixel + tmpText);
countPixel++;
countPixel++;
tmpPixel=(byte) (this.originalImageData[countPixel] & 0x7f);
tmpText=(byte) ((textByte & 0x20)<<2);
this.targetImageData[countPixel]=(byte) (tmpPixel + tmpText);
countPixel++;
countPixel++;
tmpPixel=(byte) (this.originalImageData[countPixel] & 0x7f);
tmpText=(byte) ((textByte & 0x40)<<1);
this.targetImageData[countPixel]=(byte) (tmpPixel + tmpText);
countPixel++;
countPixel++;
tmpPixel=(byte) (this.originalImageData[countPixel] & 0x7f);
tmpText=(byte) (textByte & 0x80);
this.targetImageData[countPixel]=(byte) (tmpPixel + tmpText);
countPixel++;
countPixel++;
countText++;
}
Utility.extractInt(this.forReserved,this.bh.bf,6);
}
/**
* To hide info into 24bit bmp file
* must do pre-work: setBMP and setTExt and isAdequate and getMap24 and setTargetBMP;
*
*/
public void hideInfo24()
{
this.targetImageData=new byte[this.originalImageData.length];
for (int i=0;i<this.originalImageData.length;i++) this.targetImageData[i]=this.originalImageData[i];
int countPixel=0;
int countText=0;
while(countText<this.originalTextData.length)
{
byte textByte=this.originalTextData[countText];
byte tmpText;
byte tmpPixel;
// System.out.print("lenth="+this.originalImageData.length);
tmpPixel=(byte) (this.originalImageData[countPixel] & 0xfc);
tmpText=(byte) (textByte & 0x03);
this.targetImageData[countPixel]=(byte) (tmpPixel + tmpText);
countPixel++;
tmpPixel=(byte) (this.originalImageData[countPixel] & 0xfc);
tmpText=(byte) ((textByte & 0x0c)>>2);
this.targetImageData[countPixel]=(byte) (tmpPixel + tmpText);
countPixel++;
tmpPixel=(byte) (this.originalImageData[countPixel] & 0xfc);
tmpText=(byte) ((textByte & 0x30)>>4);
this.targetImageData[countPixel]=(byte) (tmpPixel + tmpText);
countPixel++;
tmpPixel=(byte) (this.originalImageData[countPixel] & 0xfc);
tmpText=(byte) ((textByte & 0xc0)>>6);
this.targetImageData[countPixel]=(byte) (tmpPixel + tmpText);
countPixel++;
countText++;
}
Utility.extractInt(this.forReserved,this.bh.bf,6);
}
/**
* To hide info into 32bit bmp file
* must do pre-work: setBMP and setTExt and isAdequate and getMap32 and setTargetBMP;
*
*/
public void hideInfo32()
{
this.targetImageData=new byte[this.originalImageData.length];
for (int i=0;i<this.originalImageData.length;i++) this.targetImageData[i]=this.originalImageData[i];
int countPixel=0;
int countText=0;
while(countText<this.originalTextData.length)
{
byte textByte=this.originalTextData[countText];
byte tmpText;
byte tmpPixel;
tmpPixel=(byte) (this.originalImageData[countPixel] & 0xfc);
tmpText=(byte) (textByte & 0x03);
this.targetImageData[countPixel]=(byte) (tmpPixel + tmpText);
countPixel++;
tmpPixel=(byte) (this.originalImageData[countPixel] & 0xfc);
tmpText=(byte) ((textByte & 0x0c)>>2);
this.targetImageData[countPixel]=(byte) (tmpPixel + tmpText);
countPixel++;
tmpPixel=(byte) (this.originalImageData[countPixel] & 0xfc);
tmpText=(byte) ((textByte & 0x30)>>4);
this.targetImageData[countPixel]=(byte) (tmpPixel + tmpText);
countPixel++;
tmpPixel=(byte) (this.originalImageData[countPixel] & 0xfc);
tmpText=(byte) ((textByte & 0xc0)>>6);
this.targetImageData[countPixel]=(byte) (tmpPixel + tmpText);
countPixel++;
countText++;
}
Utility.extractInt(this.forReserved,this.bh.bf,6);
}
//write the targetBMPFile //写目的图像文件
public void writeTargetBMP() throws IOException
{
fos.write(this.bh.bf,0,14);
fos.write(this.bh.bi,0,40);
fos.write(this.targetImageData,0,this.targetImageData.length);
fos.flush();
fos.close();
// System.out.println("this.originalImageData.length="+this.originalImageData.length);
// System.out.println("targetFile.length-54= "+(this.targetBMP.length()-54));
}
/**
* the operation method for outside
* Do no check to the File object transferred in
*/
public static int hide(File originalBMP , File originalTxt, File targetBMP )//这个函数被外界调用,前面的函数都不被外界用
{
if (originalBMP.equals(targetBMP) || originalTxt.equals(targetBMP) || originalBMP.equals(originalTxt)) return 1;
//不许源文件和目的文件重复
InfoHiding ih=new InfoHiding();
try{
ih.setOriginalBMP(originalBMP);
}catch (Exception e)
{
//e.printStackTrace();
return 21; //Fault 21: Fail to create stream for original BMP // File not Found
}
try {
ih.getBMPHeaderInfo();
} catch (IOException e2) {
//e2.printStackTrace();
return 28; //Fault 28: Fail to get the header info
}
if(ih.bh.isBM==false) return 31; // Fault 31: not BMP file
if(ih.bh.nreserved>0) return 32; // Fault 32: not original
if(ih.bh.ncompression!=0) return 33; //Fault 33: Compressed BMP File
if(ih.bh.nbitcount<16 ) return 34; //Fault35: color bit too low to hide info.
try {
ih.setOriginalText(originalTxt);
} catch (Exception e1) {
e1.printStackTrace();
return 22 ; //Fault 22: Fail to create stream for original TXT
}
try {
ih.getText();
} catch (IOException e) {
// e.printStackTrace();
return 42; //Fault 42: Fail to read Text from stream (IOException)
}
//对不痛制式分别讨论
switch (ih.bh.nbitcount)
{
case 16:
try {
ih.readMap16();
} catch (IOException e) {
// TODO 自动生成 catch 块
// e.printStackTrace();
return 41; //Fault 41: Fail to read BMP from stream (IOException)
}
if (ih.isAdequate16()==false) return 51; //Fault 51 : BMP capacity is not adequate for TXT content
try {ih.hideInfo16();}catch (Exception e ){return 51;}
break;
case 24:
try {
ih.readMap24();
} catch (IOException e) {
// TODO 自动生成 catch 块
// e.printStackTrace();
return 41; //Fault 41: Fail to read BMP from stream (IOException)
}
if (ih.isAdequate24()==false) return 51; //Fault 51 : BMP capacity is not adequate for TXT content
try {ih.hideInfo24();}catch (Exception e ){return 51;}
break;
case 32:
try {
ih.readMap32();
} catch (IOException e) {
// TODO 自动生成 catch 块
// e.printStackTrace();
return 41; //Fault 41: Fail to read BMP from stream (IOException)
}
if (ih.isAdequate32()==false) return 51; //Fault 51 : BMP capacity is not adequate for TXT content
try {ih.hideInfo32();}catch (Exception e ){return 51;}
break;
default :
return 34; //Fault 34: Invalid Color Bit number
}
//Section Output
try {
ih.setTargetBMP(targetBMP);
} catch (FileNotFoundException e) {
//e.printStackTrace();
return 23; //Fault 23: Fail to open and set stream for target BMP file
}
try {
ih.writeTargetBMP();
} catch (IOException e) {
//e.printStackTrace();
return 43; //Fault 43: Fail to write to target BMP file : IOException
}
return 0;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?