簡體   English   中英

為什么從圖像字節數組開始,位圖始終為空?

[英]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。

這是您需要做的:

  1. 從文件讀取映像字節,例如fileArray。
  2. 將fileArray解碼為實際像素imageArray
  3. 操縱imageArray中的像素
  4. 再次將像素編碼為圖像格式(例如png),例如newFileArray。
  5. 將newFileArray存儲到文件中。

您似乎正在做的是嘗試直接操作fileArray中的字節,從而破壞了文件格式,並且無法將字節解碼為像素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM