![](/img/trans.png)
[英]If convert from byte array to bitmap object return null value. Why?
[英]Why the Bitmap is always null, from image byte array?
我有一个问题,无法在我的应用程序中解决。 应用程序对图像(如PNG)执行操作, 将图像转换为字节数组 , 然后 对字节数组 的一部分进行按位运算 ,问题是新系列的新位图格式字节始终为null。 我只是不明白为什么来自新数组字节的新位图始终为null,并且不知道如何解决此错误。
// 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);
}
好像您正在尝试在图像的低位编码文本消息(如果我正确理解您的代码)。 我今年实际上将其用作其他极客的圣诞贺卡。
但是,在创建Text时,会将文本编码到图像文件的byte []中,从而可能会破坏图像(除非您非常幸运)。 您可能希望添加文本字节在解码图像上(Bitmap _bitmapunu)。
Bitmap.decodeByteArray的javadoc说,如果无法解码图像,它将返回null。
这是您需要做的:
您似乎正在做的是尝试直接操作fileArray中的字节,从而破坏了文件格式,并且无法将字节解码为像素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.