我试图将消息写入保存在用户手机上的图像,基本上我的应用应该做的是,将图像视图设置为我刚创建的图像视图,(我将其设置为添加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!" />
答案 0 :(得分:1)
您永远不会使用修改后的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))
。