Android image editing and processing (1)

Source: Internet
Author: User

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 ..


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.