Imageview在android中解码后显示黑色图像

时间:2014-02-21 13:59:18

标签: android image android-imageview android-image

我正在使用Android中的Image steganography。为此,我需要将图像转换为位数组并将其解码回来。但是当我尝试将图像转换回原始形状时,它在我的ImageView中只显示黑色。这是我的代码

btnEncode = (Button) findViewById(R.id.encode);
    btnEncode.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            //imgPath.setText(imageToBase64(selectedImagePath));
            ImageView imageView=(ImageView)findViewById(R.id.imageView1);
            BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
            Bitmap bitmap = drawable.getBitmap();

              bytes = getBytesFromBitmap(bitmap);
              StringBuilder binary = new StringBuilder();
              for (byte b : bytes)
              {
                 int val = b;
                 for (int i = 0; i < 8; i++)
                 {
                    binary.append((val & 128) == 0 ? 0 : 1);
                    val <<= 1;
                 }
                 binary.append(' ');
              }

              //To save the binary in newString

            String ImageEncoded=new String(binary.toString());
            TextView imgData=(TextView)findViewById(R.id.txtResult);
            imgData.setText(ImageEncoded);
        }
    });
    btnDecode = (Button) findViewById(R.id.decode);
    btnDecode.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            ImageView imageView=(ImageView)findViewById(R.id.imageView1);
            BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
            Bitmap bitmap = drawable.getBitmap();

              bytes = getBytesFromBitmap(bitmap);

            Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
            ImageView image = (ImageView) findViewById(R.id.imageView2);

            image.setImageBitmap(bmp);
        }
    });

public static byte[] getBytesFromBitmap(Bitmap bitmap)
{
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 70, stream);
    return stream.toByteArray();
}

1 个答案:

答案 0 :(得分:2)

关于你的转换器格式。使用CompressFormat.PNG代替CompressFormat.JPEG。这是由“JPEG不像PNG那样做透明度”引起的。