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

问题描述 投票:-2回答:1

我有一个形象的隐藏秘密信息后的颜色问题,我更新的代码,通过上传完整的代码,请帮帮我,我在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

android bitmap
1个回答
0
投票

问题是,当你所期望的功能srcImg.getPixel(i, j)不返回阿尔法颜色值。它返回整个颜色(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));
© www.soinside.com 2019 - 2024. All rights reserved.