我的位图颜色有问题 - 如何解决这个问题



隐藏秘密消息后图像颜色有问题,我更新代码,通过上传完整代码,请帮助我,我在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);

源图片

结果图像

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

最新更新