【问题标题】:Problem in my bitmap colors - how to fix that?我的位图颜色有问题 - 如何解决?
【发布时间】:2019-02-06 14:43:49
【问题描述】:

隐藏秘密消息后图像颜色有问题,我更新代码,通过上传完整代码,请帮助我,我在 android 的第一步。

//从drawable中获取PNG图片

drawable = ContextCompat.getDrawable(this, R.drawable.penguins1);
    BitmapDrawable abmp = (BitmapDrawable) drawable;
    //***the original image
    srcImg = abmp.getBitmap();

    //this image resulted after hide message
    newimage = Bitmap.createBitmap(srcImg.getWidth(), srcImg.getHeight(),Bitmap.Config.ARGB_8888 );
//this function contain the settings of image and arrays
set_hide();
//this function for hidding operation` 
Least_Hide_fract();
//the following code for converting 3 resulted arrays into bmp image
for (int i = 0; i < newimage.getWidth(); i++) {
    for (int j = 0; j < newimage.getHeight(); j++) {

        byte Red = (Range_R[i][j]);
        byte Green = (Range_G[i][j]);
        byte Blue = (Range_B[i][j]);
        byte alpha = (byte) alpha(srcImg.getPixel(i,j));
                newimage.setPixel(i, j, Color.argb(alpha, Red, Green, Blue));
            }}
imagev.setImageBitmap(newimage);

  //save image after hiding to file
String folder_main = "saveimage";
File dir = new File(Environment.getExternalStorageDirectory(),folder_main);
if(!dir.exists()) {dir.mkdirs();}
File  file = new File(dir,"secretimage.png");

try{
    OutputStream stream ;
    stream = new FileOutputStream(file);
    newimage.compress(Bitmap.CompressFormat.PNG,100,stream);
    stream.flush();
    stream.close();
}catch (IOException e)
{
    e.printStackTrace();
}

// Parse the saved image path to uri
 Uri savedImageURI = Uri.parse(file.getAbsolutePath());

// Display the saved image to ImageView
 imagev.setImageURI(savedImageURI);

// Display saved image uri to TextView
tv_saved.setText("Image saved in external storage.\n" + savedImageURI);

source image

result image

【问题讨论】:

  • 评论不用于扩展讨论;这个对话是moved to chat。如果您被要求提供更多信息,请在问题中edit它。

标签: android bitmap


【解决方案1】:

问题是函数srcImg.getPixel(i, j) 没有像您期望的那样返回alpha 颜色值。它以整数形式返回整个颜色 (ARGB)。

所以你首先要提取int的alpha值:

int alpha = alpha(srcImg.getPixel(i, j));
int myAlpha = (alpha >> 24) & 0xff;
newimage.setPixel(i, j, Color.argb( myAlpha, Red, Green, Blue));

【讨论】:

  • 我试过你的答案,但没有改变......还有其他想法吗?
猜你喜欢
  • 2021-08-13
  • 2021-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-26
  • 2018-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多