[英]If convert from byte array to bitmap object return null value. Why?
I'm trying to develop an application in Android and I'm having a problem I can't figure out how to solve. 我正在尝试在Android中开发应用程序,但遇到了一个无法解决的问题。
Description: 描述:
The application consists of image processing and one of the routines is as follows. 该应用程序包含图像处理,例程之一如下。 An image file (PNG) is converted into a array of bytes databyteimage[] with n elements, a part of this array ex: from databyteimage[i] to databyteimage[i+k] consecutive with k elements and " i " is offset databyteimage[], the LSB (Least Significant Bit) is replaced, the value what is replaced coms from other array of bytes ex:datareplace[] with m elements the value of k is m*8.
图像文件(PNG)转换为具有n个元素的字节数组databyteimage [],该数组的一部分ex:从databyteimage [i]到连续有k个元素的databyteimage [i + k],而“ i”是偏移量databyteimage [],将替换LSB(最低有效位),该值替换为m个元素,其他字节数组ex:datareplace []的coms,k的值为m * 8。 This operation is done using operations on bits .
使用bit上的操作完成此操作。 After this process, a new string databyteimage[] is created.
此过程之后,将创建一个新的字符串databyteimage []。
The problem: 问题:
When trying to create the BITMAP object from the new array databyteimage[] returns NULL to displaty or show the new image. 尝试从新数组中创建BITMAP对象时,databyteimage []返回NULL以显示或显示新图像。
I would appreciate if you could help me find a solution to this problem, since until now no one could help me. 如果您能帮助我找到解决此问题的方法,将不胜感激,因为到目前为止,没有人可以帮助我。
***// 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);
}
What you are reading in getByteImageData is not a bitmap. 您在getByteImageData中读取的不是位图。 It is a file, most likely a compressed image.
它是一个文件,很可能是压缩图像。 Working on the bytes from this file is very different from working on the image pixels.
处理此文件中的字节与处理图像像素非常不同。 I suggest you work on the actual Bitmap object:
我建议您使用实际的Bitmap对象:
Load the 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);
Modify a pixel: 修改像素:
int pixelRGB = mutable.getPixel(x, y);
// Do whatever you have to do
mutable.setPixel(x, y, pixelRGB);
Write it back: 写回去:
mutable.compress(Bitmap.CompressFormat.PNG, 100, new ByteArrayOutputStream(new FileOutputStream(_newFileImage)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.