我的位图颜色出现问题-如何解决?

时间:2019-02-06 14:43:50

标签: android bitmap

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

//此从可绘制对象获取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

1 个答案:

答案 0 :(得分:0)

问题是函数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));