java - 隐写术尝试,为什么我的照片没有任何反应?

标签 java android imageview steganography

我正在尝试向用户手机上保存的图像写一条消息,基本上我的应用程序应该做的是,将 ImageView 设置为我刚刚创建的 View ,(我将其设置为将 100 添加到其他像素所以我可以告诉)但是这并没有发生,我希望你能告诉我为什么以及如何修复它。谢谢!

代码:

public class EncryptImg extends ActionBarActivity implements OnClickListener{

private static int LOAD_IMAGE_RESULTS = 1;
ImageView imgEncrypt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.encryptimg);

    Button galleryBrowse =  (Button) findViewById(R.id.browseGallerybtn1);
    galleryBrowse.setOnClickListener(this); /*Set OnClickListener to listen for the galerryBrowse button*/

    Button encodeImage =  (Button) findViewById(R.id.encodeBtn);
    encodeImage.setOnClickListener(this);   /* Set OnClickListener for the encodeBtn button */

}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    if(v.getId() == R.id.browseGallerybtn1){
        Intent loadImgIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
        startActivityForResult(loadImgIntent, LOAD_IMAGE_RESULTS);

    if(v.getId() == R.id.encodeBtn){

        encodeImg(imgEncrypt);

    }

    }

}
 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        imgEncrypt = (ImageView) findViewById(R.id.encryptImgView);

        // http://www.itcuties.com/android/pick-image-from-gallery/.

        if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {
            // Let's read picked image data - its URI
            Uri pickedImage = data.getData();
            // Let's read picked image path using content resolver
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
            cursor.moveToFirst();
            String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

            // Now we need to set the GUI ImageView data with data read from the picked file.
            imgEncrypt.setImageBitmap(BitmapFactory.decodeFile(imagePath));

            // At the end remember to close the cursor or you will end with the RuntimeException!
            cursor.close();
        }
    }
 /*Added code */
 public void encodeImg(ImageView imgEncrypt){
     String test = "hello";

     Bitmap bmap;
     BitmapDrawable bmapD = (BitmapDrawable)imgEncrypt.getDrawable();
     bmap = bmapD.getBitmap();


     Bitmap operation = Bitmap.createBitmap(bmap.getWidth(),bmap.getHeight(),bmap.getConfig());

     for(int i=0; i<bmap.getWidth(); i++){
         for(int j=0; j<5;j++){

             int p = bmap.getPixel(i, j);

             int r = Color.red(p);
             int g = Color.green(p);
             int b = Color.blue(p);
             int alpha = Color.alpha(p);

             int value = Character.getNumericValue(test.charAt(i));

             r = 100  + r;
             g = 100  + g;
             b = 100  + b;
             Log.w("myApp", "code has executed");
             alpha = value;
             operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));


         }
     }

     imgEncrypt.setImageBitmap(operation);

 }
}

XML:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/browseGallerybtn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="19dp"
    android:layout_marginTop="20dp"
    android:text="Browse Gallery" />

<ImageView
    android:id="@+id/encryptImgView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:src="@drawable/ic_launcher" />

<Button
    android:id="@+id/encodeBtn"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="16dp"
    android:layout_marginRight="14dp"
    android:text="Encode!" />

最佳答案

永远不要使用修改后的 alpha 值:

  alpha = value;  // your modified value
  operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
                                      ^^^^^^^^^^^^^---the original alpha

您可能需要 .setPixel(i, j, Color.argb(alpha, r, g, b))

关于java - 隐写术尝试,为什么我的照片没有任何反应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28608603/

相关文章:

Android Activity 不进入 onBackPressed() 方法

java - Docker-Compose - 控制 docker-compose 执行顺序

android - 如何在 fragment 的编辑文本中保存输入值?

java - jackson 列表反序列化。嵌套列表

android - Dagger 2 – 我应该使用单例 Realm 实例吗?

java - 拍摄照片并在 Android 的 ImageView 中以全尺寸显示

安卓可拖动ImageVIew

java - 拖放流离失所

java - 在 JOptionPane 中打印数组

java - 为什么对 treeSet 使用自定义 Comparator 会破坏 String 对象的相等性?