我遇到了问题,无法在我的申请中解决。应用程序对像PNG这样的图像执行操作,图像以字节数组转换,然后按位操作执行此字节数组中的一块,问题是新系列的新位图格式字节始终为空。我只是不明白为什么来自新数组字节的新位图始终为空,并且不知道如何解决这个错误。
// GetByte method from Image
private byte[] getByteImageData(String filePath) {
/*
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
Bitmap mutable = bitmap.copy(Bitmap.Config.RGB_565, true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mutable.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
*/
byte[] _imagebytedata = new byte[1024];
InputStream _input = null;
try {
if (filePath != null && (filePath.length() > 0)) {
// Create a file for image
File _fileimage = new File(filePath);
if (_fileimage.exists()) {
// Get the byte from file image
_input = new BufferedInputStream(new FileInputStream(
_fileimage));
_imagebytedata = new byte[(int) _fileimage.length()];
_input.read(_imagebytedata, 0, (int) _fileimage.length());
_input.close();
}
}
} catch (Exception e) {
}
// Bitwise operations
private byte[] Text(byte[] imagedata, byte[] textmess, int offset) {
for (int i = 0; i < textmess.length; ++i) {
int add = textmess[i];
for (int bit = 7; bit >= 0; --bit, ++offset) {
int b = (add >>> bit) & 1;
imagedata[offset] = (byte) ((imagedata[offset] & 0xFE) |b);
}
}
return imagedata;
}
//Save image from new byte array
private boolean saveImage(String pathFile,byte[] encodedimage) {
OutputStream _output = null;
File _newFileImage = new File(pathFile);
byte[] _encodedimage = encodedimage;
//Bitmap _imagebitmap = BitmapFactory.decodeByteArray(encodedimage, 0, encodedimage.length);
if (_newFileImage.exists()) {
try {
_output = new BufferedOutputStream(new FileOutputStream(
_newFileImage));
_output.write(_encodedimage, 0, _encodedimage.length);
_output.flush();
_output.close();
return true;
} catch (Exception e) {
}
;
}// _newFileImage.exists()
return false;
}
public boolean encodeTextInFile(String filepath, String text) {
byte[] _newimagebytedata;
byte[] _imagebytedata = getByteImageData(filepath);
byte[] _textbytedata = text.getBytes();
byte[] _lengthbytedata = byteConversion(text.length());
Bitmap _bitmapunu = BitmapFactory.decodeByteArray(_imagebytedata, 0, _imagebytedata.length);
_newimagebytedata = Text(_imagebytedata, _lengthbytedata, 33);
Bitmap _bitmapdoi = BitmapFactory.decodeByteArray(_newimagebytedata, 0, _newimagebytedata.length);
// The value of variable _bitmapdoi is null
_newimagebytedata = Text(_imagebytedata, _textbytedata, 65);
return saveImage(filepath, _newimagebytedata);
}
答案 0 :(得分:0)
看起来您正在尝试在图像的低位编码文本消息(如果我正确理解您的代码)。今年我实际上将它作为圣诞贺卡用于同伴们。
但是,当您创建Text时,您将文本编码为图像文件的byte [],因此可能会破坏图像(除非您非常幸运)。您可能希望将文本字节添加到解码图像上(Bitmap _bitmapunu)。
Bitmap.decodeByteArray的javadoc表示如果图像无法解码,它将返回null。
这是你需要做的:
您似乎正在尝试直接操作 fileArray 中的字节,从而破坏文件格式并使其无法将字节解码为像素。