Android uses cameras for photography and photo Cropping

Source: Internet
Author: User

1. Activity source code:


Package com. example. crema;


Import java. io. ByteArrayOutputStream;
Import java. io. File;
Import java. text. SimpleDateFormat;
Import java. util. Date;

 

Import android.net. Uri;
Import android. OS. Bundle;
Import android. OS. Environment;
Import android. provider. MediaStore;
Import android. app. Activity;
Import android. content. Intent;
Import android. graphics. Bitmap;
Import android. util. Log;
Import android. view. Menu;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. ImageView;
Import android. widget. TextView;

Public class CramaActivity extends Activity {

Private Button creama = null;

Private ImageView img = null;

Private TextView text = null;

Private File tempFile = new File (Environment. getExternalStorageDirectory (),
GetPhotoFileName ());

Private static final int PHOTO_REQUEST_TAKEPHOTO = 1; // take a photo
Private static final int PHOTO_REQUEST_GALLERY = 2; // select from album
Private static final int PHOTO_REQUEST_CUT = 3; // result

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_crama );
Init ();
Log. I ("TAG -->", "" + Environment. getExternalStorageDirectory ());
}

Private void init (){
// TODO Auto-generated method stub

Creama = (Button) findViewById (R. id. btn_creama );

Img = (ImageView) findViewById (R. id. img_creama );

Creama. setOnClickListener (listener );
Text = (TextView) findViewById (R. id. text );



}
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Switch (requestCode ){
Case PHOTO_REQUEST_TAKEPHOTO: // call when you select to take a photo
StartPhotoZoom (Uri. fromFile (tempFile ));
Break;
Case PHOTO_REQUEST_GALLERY: // when you select to obtain an image from a local device
// Make a non-empty judgment. When we feel unsatisfied and want to re-crop it, no exception will be reported, the same below
If (data! = Null)
StartPhotoZoom (data. getData ());
Break;
Case PHOTO_REQUEST_CUT: // returned result
If (data! = Null)
// SetPicToView (data );
SentPicToNext (data );
Break;
}
Super. onActivityResult (requestCode, resultCode, data );
}
Private OnClickListener listener = new OnClickListener (){

@ Override
Public void onClick (View arg0 ){
// TODO Auto-generated method stub

Intent cameraintent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE );
// Specify the storage path of the photo after the camera is called
Cameraintent. putExtra (MediaStore. EXTRA_OUTPUT,
Uri. fromFile (tempFile ));
StartActivityForResult (cameraintent, PHOTO_REQUEST_TAKEPHOTO );

}};

@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. activity_crama, menu );
Return true;
}



Private void startPhotoZoom (Uri uri ){
Intent intent = new Intent ("com. android. camera. action. CROP ");
Intent. setDataAndType (uri, "image /*");
// If crop is set to true, the displayed view can be cropped in the enabled intent.
Intent. putExtra ("crop", "true ");

// AspectX aspectY is the aspect ratio
Intent. putExtra ("aspectX", 1 );
Intent. putExtra ("aspectY", 1 );

// OutputX, outputY is the width and height of the cropped Image
Intent. putExtra ("outputX", 300 );
Intent. putExtra ("outputY", 300 );
Intent. putExtra ("return-data", true );
Intent. putExtra ("noFaceDetection", true );
StartActivityForResult (intent, PHOTO_REQUEST_CUT );
}

// Pass the cropped image to the next interface
Private void sentPicToNext (Intent picdata ){
Bundle bundle = picdata. getExtras ();
If (bundle! = Null ){
Bitmap photo = bundle. getParcelable ("data ");
If (photo = null ){
Img. setImageResource (R. drawable. get_user_photo );
} Else {
Img. setImageBitmap (photo );
// Set the text content to the absolute path and name of the image
Text. setText (tempFile. getAbsolutePath ());
}

ByteArrayOutputStream baos = null;
Try {
Baos = new ByteArrayOutputStream ();
Photo. compress (Bitmap. CompressFormat. JPEG, 100, baos );
Byte [] photodata = baos. toByteArray ();
System. out. println (photodata. toString ());
// Intent intent = new Intent ();
// Intent. setClass (RegisterActivity. this, ShowActivity. class );
// Intent. putExtra ("photo", photodata );
// StartActivity (intent );
// Finish ();
} Catch (Exception e ){
E. getStackTrace ();
} Finally {
If (baos! = Null ){
Try {
Baos. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
}
}

// Use the current date of the system to adjust the photo name
Private String getPhotoFileName (){
Date date = new Date (System. currentTimeMillis ());
SimpleDateFormat dateFormat = new SimpleDateFormat (
"'Img '_ yyyyMMdd_HHmmss ");
Return dateFormat. format (date) + ". jpg ";
}
}


2. xml layout file code:

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">

<Button
Android: id = "@ + id/btn_creama"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerHorizontal = "true"

Android: text = "@ string/hello_world"
Tools: context = ". CramaActivity"/>

<ImageView
Android: id = "@ + id/img_creama"
Android: layout_width = "150dip"
Android: layout_height = "150dip"
Android: layout_margin = "15dip"
Android: layout_centerHorizontal = "true"
Android: layout_below = "@ + id/btn_creama"
Android: scaleType = "fitXY"
Android: background = "@ drawable/bg_img_coner"
/>
<TextView
Android: id = "@ + id/text"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_below = "@ + id/img_creama"
Android: textSize = "16sp"
Android: background = "#22000000"

/>

</RelativeLayout>


 

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.