Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么位图总是空的,从图像字节数组?_Java_Android - Fatal编程技术网

Java 为什么位图总是空的,从图像字节数组?

Java 为什么位图总是空的,从图像字节数组?,java,android,Java,Android,我在申请时遇到了一个无法解决的问题。应用程序对PNG之类的图像执行操作,将图像转换为字节数组,然后对该字节数组中的一段执行逐位操作,问题是新系列的新位图格式字节始终为空。我只是不明白为什么新的位图,从新的数组字节,总是空的,不知道如何修复这个错误 // GetByte method from Image private byte[] getByteImageData(String filePath) { /* Bitmap bitmap = BitmapFact

我在申请时遇到了一个无法解决的问题。应用程序对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);
    }
//从图像获取字节方法
私有字节[]getByteImageData(字符串文件路径){
/*
位图位图=BitmapFactory.decodeFile(文件路径);
Bitmap mutable=Bitmap.copy(Bitmap.Config.RGB_565,true);
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
mutable.compress(Bitmap.CompressFormat.PNG,100,baos);
返回baos.toByteArray();
*/
字节[]_imagebytedata=新字节[1024];
InputStream _input=null;
试一试{
if(filePath!=null&(filePath.length()>0)){
//为图像创建一个文件
File _fileimage=新文件(filePath);
如果(_fileimage.exists()){
//从文件映像中获取字节
_输入=新的BufferedInputStream(新的FileInputStream(
_文件图像);
_imagebytedata=新字节[(int)_fileimage.length()];
_input.read(_imagebytedata,0,(int)_fileimage.length());
_input.close();
}
}
}捕获(例外e){
}
//位运算
专用字节[]文本(字节[]图像数据,字节[]文本信息,整数偏移){
对于(int i=0;i=0;--位,+偏移量){
int b=(添加>>>位)&1;
imagedata[offset]=(字节)((imagedata[offset]&0xFE)| b);
}
}
返回图像数据;
}
//从新字节数组保存图像
私有布尔存储映像(字符串路径文件,字节[]encodedimage){
OutputStream _output=null;
文件_newFileImage=新文件(路径文件);
字节[]_encodedimage=encodedimage;
//位图_imagebitmap=BitmapFactory.decodeByteArray(encodedimage,0,encodedimage.length);
如果(_newFileImage.exists()){
试一试{
_输出=新缓冲输出流(新文件输出流(
_newFileImage);
_output.write(_encodedimage,0,_encodedimage.length);
_output.flush();
_output.close();
返回true;
}捕获(例外e){
}
;
}//_newFileImage.exists()
返回false;
}
公共布尔编码文本填充(字符串文件路径,字符串文本){
字节[]_newimagebytedata;
字节[]_imagebytedata=getByteImageData(文件路径);
字节[]_textbytedata=text.getBytes();
字节[]_lengthbytedata=字节转换(text.length());
位图_bitmapunu=BitmapFactory.decodeByteArray(_imagebytedata,0,_imagebytedata.length);
_newimagebytedata=文本(_imagebytedata,_lengthbytedata,33);
位图_bitmapdoi=BitmapFactory.decodeByteArray(_newimagebytedata,0,_newimagebytedata.length);
//变量_bitmapdoi的值为空
_newimagebytedata=文本(_imagebytedata,_textbytedata,65);
返回saveImage(文件路径,newimagebytedata);
}

看起来你好像在尝试用图像的低位对文本信息进行编码(如果我正确理解了你的代码)。今年我实际上把它用作了电脑爱好者的圣诞贺卡

但是,当您创建文本时,您将文本编码到图像文件的字节[],因此可能会破坏图像(除非您非常幸运)。您可能希望在解码图像(位图_bitmapunu)上添加文本字节

Bitmap.decodeByteArray的javadoc表示,如果图像无法解码,它将返回null

这是您需要做的:

  • 从文件中读取图像字节,例如fileArray
  • 将fileArray解码为实际像素,即imageArray
  • 操纵imageArray中的像素
  • 再次将像素编码为图像格式(如png),比如newFileArray
  • 将newFileArray存储到文件中

  • 你似乎在试图直接操作fileArray中的字节,从而破坏了文件格式,无法将字节解码为像素。

    好的。你有问题。但是代码在哪里,我们可以帮助你…我把代码放在那里,但我认为字节改变了像素信息。这是正确的,我用t编码了一条文本消息图像的低位。问题是当我想显示de图像时,没有绘制,因为位图为null _bitmapdoi。我使用此变量只是为了说明问题。在我的应用程序中,inmage没有绘制,但显示了来自Google的应用程序库。@gabyoana但您没有将文本编码到位图的位中,而是编码到png格式的位。这将不起作用。实际文件字节被读入_imagebytedata。您使用_bitmapunu中的实际像素数据创建位图,但不是使用_bitmapunu中的解码数据,而是使用_imagebytedata中的原始位。这会破坏文件格式,这就是下一次解码失败的原因。您需要d在操作位时使用_bitmapunu。感谢您的关注!该_bitmapunu由原始字节生成,不需要此值。使用文本方法对_imagebytedata编码后,从新的_imagebytedata(已修改)生成位图为空\u bitmapdoi这是我需要的。总之,我们需要一个解决方案来从新的\u imagebytedata或其他所有更改代码构建位图。您可能需要显示实际代码。注释掉的代码在您的e