2012-02-27 8 views
0

私はAndroidでアプリケーションを開発しようとしていますが、私は問題を抱えています。解決する方法を理解できません。バイト配列からビットマップオブジェクトに変換する場合、null値が返されます。理想的な理由がありますか?

説明:

アプリケーションは、画像処理で構成され、以下のようにルーチンのものです。画像ファイル(PNG)は、n個の要素からなるdatabyteimage []のバイト配列に変換され、この配列の一部は、k個の要素と連続するdatabyteimage [i]からdatabyteimage [i + k]に、そして "i"はオフセットdatabyteimage []、LSB(Least Significant Bit)が置き換えられ、置き換えられる値は他のバイト配列から得られる。例:mの要素を持つdatareplace []、kの値はm * 8である。この操作はビット操作を使用して行われます。このプロセスの後、新しい文字列databyteimage []が作成されます。

問題:

[]戻りdisplaty又は新たな画像を表示するためにNULL新しいアレイdatabyteimageからビットマップオブジェクトを作成しようとしています。

今まで誰も私を助けなかったので、私がこの問題の解決策を見つけるのを手伝っていただければ幸いです。

***// 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 to change LSB of byte array image** 

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()); 


     _newimagebytedata = Text(_imagebytedata, _lengthbytedata, 33); 
     _newimagebytedata = Text(_imagebytedata, _textbytedata, 65); 

    **// The value of variable _bitmapdoi is null here is the problem** 

Bitmap _bitmapdoi = BitmapFactory.decodeByteArray(_newimagebytedata, 0,_newimagebytedata.length); 
     return saveImage(filepath, _newimagebytedata); 
    } 
+0

を私たち提示してくださいあなたのコードの適切な部分は、[SSCCE](http://sscce.org)として好まれます。そうでなければ、何がうまくいかないのか推測できます。 – DNA

+0

少なくとも投稿:pngをバイトに変換する部分、バイトの扱い、バイトからビットマップを作成する部分。 – njzk2

+0

@DNAこんにちは私はコードを入れて、私はどこに問題があるかを記述するコードにコメントを追加します。しかし、私はアンドロイドdosentのビットマップオブジェクトは、グレースケールをサポートすると思います。 – gabyoana

答えて

0

getByteImageDataで読んでいるものは、ビットマップではありません。これはファイルであり、おそらく圧縮された画像です。このファイルのバイトを処理することは、イメージピクセルを処理することとは非常に異なります。ピクセルを変更

Bitmap bitmap = BitmapFactory.decodeFile(filePath); 
// Not quite sure if the returned bitmap is mutable, so 
Bitmap mutable = bitmap.copy(Bitmap.Config.RGB_565, true); 

int pixelRGB = mutable.getPixel(x, y); 
// Do whatever you have to do 
mutable.setPixel(x, y, pixelRGB); 

はそれをバックライト:

は、ビットマップをロードします。私はあなたが実際のBitmapオブジェクトに取り組む示唆

mutable.compress(Bitmap.CompressFormat.PNG, 100, new ByteArrayOutputStream(new FileOutputStream(_newFileImage))); 
+0

あなたのアテンションに感謝します!投稿したコードは、画像の処理方法を理解するのに大いに役立ちましたが、画像の特定の処理を取得することには興味がありません。私のコードを見て、** Text **メソッドに注意を払うと、** textmess **変数という1つの情報を入れるために最後の半分のバイトを変更することがわかります。このメソッドは、新しいイメージを表す新しいバイト配列を返します。よろしく! – gabyoana

+0

あなたがファイルbyte []を変更すると、イメージを表さない不正なファイルになります。 – njzk2

+0

そして、1つの解決策は存在しません!! – gabyoana

関連する問題