0

I have a problem and can not solve in my application. The application performs operations on images like PNG, the image is convert in a byte array, then a piece from this array of bytes is performed on bitwise operations, the problem is the new series of new bitmap format byte is always null. I just do not understand why the new bitmap, from new array byte, is always null and not know how to fix it this bug.

// 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);
    }
2
  • 1
    Ok. You have a problem. But where is the code so we can help you ... Commented Feb 12, 2012 at 12:22
  • I put the code,but i think the byte change the pixel information Commented Feb 12, 2012 at 12:47

1 Answer 1

0

It looks as if you are trying to encode a text message in the lower bits of the image (if I understand your code correctly). I actually used this as a christmas card for fellow geeks this year.

However, when you create Text you encode the text into the byte[] of the image file thus probably destroying the image (unless you are very lucky). You probably want your addition of the text bytes to be on the decoded image (Bitmap _bitmapunu).

The javadoc for Bitmap.decodeByteArray says that it will return null if the image can not be decoded.

This is what you need to do:

  1. Read the image bytes from the file, say fileArray.
  2. Decode the fileArray into actual pixels, imageArray
  3. Manipulate the pixels in imageArray
  4. Encode the pixels into a image format again (such as png), say newFileArray.
  5. Store the newFileArray to a file.

What you seem to be doing is trying to manipulate the bytes in fileArray directly, thus breaking the file format and making it impossible to decode the bytes into pixels.

Sign up to request clarification or add additional context in comments.

4 Comments

It's corect i encode a text message in the lower bits of the image. The problem is when i whant to display de image, is not drawing beacuse the bitmap is null _bitmapdoi. I use this variable only to descover the problem. In my app the inmage is not drawing but in app Gallery from Google is show up.
@gabyoana but you do not encode the text into the bits of the bitmap, but instead into the bits of the png format. This will not work. The actual file bytes are read into the _imagebytedata. You create a Bitmap with the actual pixel data in _bitmapunu, but instead of using the decoded data from _bitmapunu you instead use the original bites from _imagebytedata. That corrupts the file format and that is why the next decode fails. You need to use the _bitmapunu when manipulating the bits.
Thanks for your attention! The _bitmapunu is made from original bytes and don't need this vlaue. After _imagebytedata is encoded with Text method, from the new _imagebytedata (which is modified) the bitmap is null _bitmapdoi this i need . In conclusion we need a solution to build bitmap from the new _imagebytedata or other change all code.
You probably need to show the actual code. The commented out code in your example indicates that you have been on the right track. However, now you seem to be working directly on the bytes in the image format, not the pixels. I'll add an outline in my answer of what you need to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.