2012-02-12 13 views
0

私は問題があり、私のアプリケーションで解決することはできません。アプリケーションは、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); 
    } 
+1

あなたは問題があります。しかし、私たちがあなたを助けることができるようにコードはどこにありますか? –

+0

私はコードを入れましたが、私はバイトがピクセル情報を変更すると思います – gabyoana

答えて

0

に見えます。私は今年、同僚のオタクのクリスマスカードとしてこれを使用しました。

しかし、テキストを作成すると、画像ファイルのバイト[]にテキストがエンコードされるため、画像が破損する可能性があります(非常に幸運な場合を除きます)。あなたはおそらく、復号化された画像(Bitmap _bitmapunu)上にテキストバイトを追加したいと思うでしょう。

Bitmap.decodeByteArrayのjavadocは、画像をデコードできない場合はnullを返します。これは、あなたが何をする必要があるかである

  1. ファイルから画像のバイトを読むには、fileArrayを言います。
  2. デコードfileArray実画素に、imageArray
  3. はimageArray
  4. における画素は(例えば、PNGなど)を再度画像フォーマットに各画素をエンコードし、操作し、newFileArray言います。
  5. newFileArrayをファイルに格納します。

何をやっているように見えることはので、ファイル形式を破壊し、それが不可能ピクセルにバイトをデコードすること、直接fileArrayでバイトを操作しようとしています。

+0

それはcorectです。私は画像の下位ビットにテキストメッセージをエンコードします。問題は、イメージを表示するのが欲しいときに、ビットマップがnull _bitmapdoiであることを示していないことです。私はこの変数を使用して問題を説明します。私のアプリでは、画像は描画されていませんが、Googleのアプリギャラリーで表示されます。 – gabyoana

+0

@ gabyoanaしかし、テキストをビットマップのビットにエンコードするのではなく、png形式のビットにエンコードします。これは動作しません。実際のファイルバイトは_imagebytedataに読み込まれます。 _bitmapunuの実際のピクセルデータでビットマップを作成しますが、_bitmapunuからのデコードされたデータを使用する代わりに_imagebytedataの元のビットを使用します。これはファイル形式を破損し、そのため次のデコードに失敗します。ビットを操作するときは、_bitmapunuを使用する必要があります。 –

+0

ご注意いただきありがとうございます! _bitmapunuは元のバイトから作成され、このvlaueは必要ありません。 _imagebytedataがTextメソッドでエンコードされた後、変更された新しい_imagebytedataから、ビットマップは必要なnull _bitmapdoiになります。結論として、新しい_imagebytedataからビットマップを構築するか、または他のすべてのコードを変更するソリューションが必要です。 – gabyoana

関連する問題