我正在使用此link中的隐写代码将信息嵌入到图像的所有ARGB频道中。代码是为java编写的,但我已经为Android修改了它,使用Bitmap来处理图像。
我将消息的长度转换为32位数字,我将其存储在前8个像素中。每像素4位,每个A,R,G和B通道的LSB中有一位。但是,当我提取消息长度的位并将它们转换为extractInteger
函数中的整数时,我得到2147483647,后来导致OutOfMemory异常。
我尝试了以下方法来找到错误的来源。
因此,我怀疑保存M'或加载M'时出现问题。后者可能会出现,因为我修改了Alpha通道的值作为嵌入过程的一部分,但是当加载M'时,它默认为255.
如果代码没有问题,我可以考虑解决这个问题吗?感谢。
保存图片功能:
private void saveImage() {
path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + "/Testing/");
date = new Date() ;
dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()) ;
filename = new File(dateFormat.format(date) + ".png");
fileName = filename.toString();
file = new File(path, fileName);
filePath = file.toString();
FileOutputStream out = null;
try {
out = new FileOutputStream(filePath);
embeddedImg.compress(Bitmap.CompressFormat.PNG, 100, out);
out.close();
Toast.makeText(this, "Saved!", Toast.LENGTH_SHORT).show();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+file.getAbsolutePath())));
} catch (Exception e) {
e.printStackTrace();
}
}
剪断了加载图像功能:
Bundle getImage = this.getIntent().getExtras();
gotImage = getImage.getString("decode");
BitmapFactory.Options op = new BitmapFactory.Options();
op.inPreferredConfig = Bitmap.Config.ARGB_8888;
try {
bitmapImg = BitmapFactory.decodeFile(gotImage, op);
newBitmapImg = bitmapImg.copy(Bitmap.Config.ARGB_8888, true);
decodeMessage();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
解码功能:
private void decodeMessage() {
int len = extractInteger(newBitmapImg, 0);
byte b[] = new byte[len];
for(int i=0; i<len; i++)
b[i] = extractByte(newBitmapImg, i*2+8);
viewMessage.setText(new String(b));
}
private int extractInteger(Bitmap img, int start) {
int maxX = img.getWidth(), maxY = img.getHeight(),
startX = start/maxY, startY = start - startX*maxY, count=0;
int length = 0;
for(int i=startX; i<maxX && count<32; i++) {
for(int j=startY; j<maxY && count<32; j++) {
int rgb = img.getPixel(i, j), bit = getBitValue(rgb, 0);
length = setBitValue(length, count, bit);
bit = getBitValue(rgb, 8); length = setBitValue(length, count+1, bit);
bit = getBitValue(rgb, 16); length = setBitValue(length, count+2, bit);
bit = getBitValue(rgb, 24); length = setBitValue(length, count+3, bit);
count = count+4;
}
}
return length;
}
private byte extractByte(Bitmap img, int start) {
int maxX = img.getWidth(), maxY = img.getHeight(),
startX = start/maxY, startY = start - startX*maxY, count=0;
byte b = 0;
for(int i=startX; i<maxX && count<8; i++) {
for(int j=startY; j<maxY && count<8; j++) {
if(j==maxY-1){
startY = 0;
}
int rgb = img.getPixel(i, j), bit = getBitValue(rgb, 0);
b = (byte)setBitValue(b, count, bit);
bit = getBitValue(rgb, 8); b = (byte)setBitValue(b, count+1, bit);
bit = getBitValue(rgb, 16); b = (byte)setBitValue(b, count+2, bit);
bit = getBitValue(rgb, 24); b = (byte)setBitValue(b, count+3, bit);
count = count+4;
}
}
return b;
}
private int getBitValue(int n, int location) { //n=messageLength, location=count
int v = n & (int) Math.round(Math.pow(2, location));
return v==0?0:1;
}
private int setBitValue(int n, int location, int bit) {
int toggle = (int) Math.pow(2, location), bv = getBitValue(n, location);
if(bv == bit)
return n;
if(bv == 0 && bit == 1){
n |= toggle;
System.out.println("n{toggle: "+n);
}else if(bv == 1 && bit == 0){
n ^= toggle;
}
return n;
}