我正在尝试在Android中开发一个应用程序,我遇到了一个问题,我无法弄清楚如何解决。
说明
该应用程序由图像处理组成,其中一个例程如下。图像文件(PNG)被转换为具有n个元素的字节databyteimage []数组,该数组ex的一部分:从databyteimage [i]到databyteimage [i + k]连续k个元素,“i”是偏移databyteimage [],LSB(最低有效位)被替换,从其他字节数组替换coms的值ex:datareplace []有m个元素,k的值是m * 8。使用位操作完成此操作。完成此过程后,将创建一个新字符串databyteimage []。
问题:
当尝试从新数组创建BITMAP对象时,databyteimage []返回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 to change LSB of byte array image**
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());
_newimagebytedata = Text(_imagebytedata, _lengthbytedata, 33);
_newimagebytedata = Text(_imagebytedata, _textbytedata, 65);
**// The value of variable _bitmapdoi is null here is the problem**
Bitmap _bitmapdoi = BitmapFactory.decodeByteArray(_newimagebytedata, 0,_newimagebytedata.length);
return saveImage(filepath, _newimagebytedata);
}
答案 0 :(得分:0)
您在getByteImageData中阅读的内容不是位图。它是一个文件,很可能是压缩图像。处理此文件中的字节与处理图像像素有很大不同。我建议你使用实际的Bitmap对象:
加载位图:
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
// Not quite sure if the returned bitmap is mutable, so
Bitmap mutable = bitmap.copy(Bitmap.Config.RGB_565, true);
修改像素:
int pixelRGB = mutable.getPixel(x, y);
// Do whatever you have to do
mutable.setPixel(x, y, pixelRGB);
写回来:
mutable.compress(Bitmap.CompressFormat.PNG, 100, new ByteArrayOutputStream(new FileOutputStream(_newFileImage)));