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

问题描述 投票:0回答:1

我现在在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();
}
android image android-imageview android-image
1个回答
2
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.