2012-02-12 76 views
0

我有问题,无法在我的应用程序中解决。该应用程序在诸如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); 
    } 
+1

确定。你有个问题。但哪一个代码,以便我们可以帮助你... – 2012-02-12 12:22:52

+0

我把代码,但我认为字节改变的像素信息 – gabyoana 2012-02-12 12:47:10

回答

0

看起来,如果你想在图像的下位编码的文本消息(如果我正确地理解你的代码)。我实际上是把它当作今年同行的圣诞贺卡。

然而,当你创建文本将文本编码成图像文件从而可能破坏图像(除非你是非常幸运的)的字节[]。您可能希望添加的文本字节位于解码图像上(位图_bitmapunu)。

为Bitmap.decodeByteArray的Javadoc说,这将返回null如果图像不能被解码。

这就是你需要做什么:

  1. 从文件中读取图像的字节数,说fileArray。
  2. 解码的fileArray成实际像素,imageArray
  3. 操纵像素imageArray
  4. 编码的像素为图像格式再次(如PNG),说newFileArray。
  5. 商店newFileArray到一个文件中。

你似乎什么做的是试图操纵字节直接fileArray,从而打破了文件格式,并使其无法字节解码成像素。

+0

这是corect我在编码图像的低位短信。问题是当我想要显示图像时,不是绘制,因为位图是null _bitmapdoi。我只用这个变量来解决问题。在我的应用程序中,inmage不是在绘制,而是在Google的应用程序库中显示出来。 – gabyoana 2012-02-12 16:26:30

+0

@gabyoana,但是您不会将文本编码到位图的位中,而是编码到png格式的位中。这不起作用。实际的文件字节被读入_imagebytedata。您可以使用_bitmapunu中的实际像素数据创建位图,但不是使用_bitmapunu中的解码数据,而是使用_imagebytedata中的原始位图。这破坏了文件格式,这就是下一次解码失败的原因。操作位时需要使用_bitmapunu。 – 2012-02-12 17:58:45

+0

感谢您的关注! _bitmapunu由原始字节组成,不需要这个vlaue。在_imagebytedata使用Text方法进行编码后,从新的_imagebytedata(已修改)位图为null _bitmapdoi这是我需要的。总之,我们需要一个解决方案来从新的_imagebytedata或其他所有更改代码来构建位图。 – gabyoana 2012-02-12 21:08:30