Android image editing and processing (1)
1. Use the built-in Gallery application to select an image:
Package com. example. testphotoedit; import java. io. fileNotFoundException; import android. app. activity; import android. content. intent; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. matrix; import android. graphics. paint; import android.net. uri; import android. OS. bundle; import android. view. display; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. imageView; public class MainActivity extends Activity implements OnClickListener {private ImageView chosenImageView, copyPicture; private Button choosePicture; private Uri imageFileUri; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. fragment_main); choosePicture = (Button) findViewById (R. id. button_chose); chosenImageView = (ImageView) findViewById (R. id. chose_picture); copyPicture = (ImageView) findViewById (R. id. copy_picture); choosePicture. setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubif (v. getId () = R. id. button_chose) {Intent chooseIntent = new Intent (Intent. ACTION_PICK, android. provider. mediaStore. images. media. EXTERNAL_CONTENT_URI); // start the Gallery application startActivityForResult (chooseIntent, 0 );}}
/* (Non-Javadoc) * @ see android. app. activity # onActivityResult (int, int, android. content. intent) ** in the returned Intent data, return the URI of the selected image */@ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper. onActivityResult (requestCode, resultCode, data); if (resultCode = RESULT_ OK) {imageFileUri = data. getData (); Display currentDisply = getWindowManager (). getdefadisplay display (); int dw = currentDisply. getWidth ()/2-100; int dh = currentDisply. getHeight ()/2-100;
try {
BitmapFactory. options BMP factory = new BitmapFactory. options (); BMP factory. inJustDecodeBounds = true; // load the image size rather than Bitmap bmp = BitmapFactory. decodeStream (getContentResolver (). openInputStream (imageFileUri), null, BMP factory); int heightRatio = (int) Math. ceil (BMP factory. outHeight/(float) dh); int widthRatio = (int) Math. ceil (BMP factory. outWidth/(float) dw); if (heightRatio> 1 & widthRatio> 1) {if (heightRatio> widthRatio) {BMP factory. inSampleSize = heightRatio;} else {BMP factory. inSampleSize = widthRatio;} BMP factory. inJustDecodeBounds = false; // load the real image bmp = BitmapFactory. decodeStream (getContentResolver (). openInputStream (imageFileUri), null, BMP factory); chosenImageView. setImageBitmap (bmp);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}}
2. Draw a bitmap on the in-place Graph
Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(),bmp.getWidth(), bmp.getConfig());Canvas canvas=new Canvas(alteredBitmap);Paint paint=new Paint();canvas.drawBitmap(bmp, 0,0, paint);copyPicture.setImageBitmap(alteredBitmap);
Note: You can leave a message if you do not understand it ..