【问题标题】:Why the Bitmap is always null, from image byte array?为什么位图始终为空,来自图像字节数组?
【发布时间】:2012-02-13 05:23:20
【问题描述】:

我有一个问题,无法在我的应用程序中解决。应用程序对 PNG 等图像执行操作,图像被转换为​​字节数组然后从这个字节数组中执行按位操作,问题是new 系列新位图格式字节始终为空。我只是不明白为什么来自新数组字节的新位图总是为空并且不知道如何修复这个错误。

// 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);
    }

【问题讨论】:

  • 好的。你有问题。但是代码在哪里,所以我们可以帮助您...
  • 我放了代码,但我认为字节改变了像素信息

标签: java android


【解决方案1】:

您似乎正在尝试在图像的较低位中编码文本消息(如果我正确理解您的代码)。今年我实际上把它用作了给极客们的圣诞贺卡。

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

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

这是你需要做的:

  1. 从文件中读取图像字节,例如 fileArray。
  2. 将 fileArray 解码为实际像素 imageArray
  3. 处理 imageArray 中的像素
  4. 将像素再次编码为图像格式(例如 png),例如 newFileArray。
  5. 将 newFileArray 存储到文件中。

您似乎正在尝试直接操作 fileArray 中的字节,从而破坏文件格式并使其无法将字节解码为像素。

【讨论】:

  • 正确的是,我在图像的低位编码文本消息。问题是当我想显示图像时,由于位图为空_bitmapdoi,所以没有绘制。我只使用这个变量来发现问题。在我的应用程序中,inmage 没有绘图,但在 Google 的应用程序库中显示。
  • @gabyoana 但您不会将文本编码为位图的位,而是编码为 png 格式的位。这行不通。实际的文件字节被读入 _imagebytedata。您使用 _bitmapunu 中的实际像素数据创建位图,但不是使用来自 _bitmapunu 的解码数据,而是使用来自 _imagebytedata 的原始位。这会破坏文件格式,这就是下一次解码失败的原因。操作位时需要使用 _bitmapunu。
  • 感谢您的关注! _bitmapunu 由原始字节组成,不需要这个值。 _imagebytedata 用 Text 方法编码后,从新的 _imagebytedata (已修改)位图为空 _bitmapdoi 这是我需要的。总之,我们需要一个解决方案来从新的 _imagebytedata 构建位图或其他更改所有代码。
  • 您可能需要显示实际代码。您示例中注释掉的代码表明您走在正确的轨道上。但是,现在您似乎正在直接处理图像格式中的字节,而不是像素。我将在我的回答中添加一个大纲,说明您需要做什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-24
  • 1970-01-01
  • 1970-01-01
  • 2013-01-14
  • 2013-07-29
相关资源
最近更新 更多