Back to project page Avatar.
The source code is released under:
GNU General Public License
If you think the Android project Avatar listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.syw.avatar; //from w ww. ja va 2 s. co m import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.app.Activity; import android.app.AlertDialog; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.TextView; import com.syw.avatar.util.ImageUtil; import com.syw.avatar.util.SLog; import com.syw.avatar.widget.ClipImageView; public class CropperActivity extends Activity{ private static final String TAG = CropperActivity.class.getSimpleName(); private AlertDialog progressDialog; private String pickWay = null; // ?????????: TAKE/PICK; private String imgCachePath = Constants.BasePhotoUrlDiskCached; private String pathTmpImage = null; private String pathPhotoTaked = null; private String pathCropperImage = null; // ???????????????; private Bitmap croppedImage; // ??????bitmap; private Bitmap initialImage; // ??bitmap; private String albumName; // ??????????; private TextView tvCancel; private TextView tvRetakePhoto; private TextView tvUseThisCropperPhoto; private ClipImageView civCropperPreview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_cropper); tvCancel = (TextView)findViewById(R.id.tv_cancel); tvRetakePhoto = (TextView)findViewById(R.id.tv_retakephoto); tvUseThisCropperPhoto = (TextView)findViewById(R.id.tv_use_this_cropper_photo); civCropperPreview = (ClipImageView)findViewById(R.id.civ_cropper_preview); findViewById(R.id.tv_cancel).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { initialImage.recycle(); startImageLocalPickActivity(); } }); findViewById(R.id.tv_retakephoto).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { initialImage.recycle(); startImageCaptureActivity(); } }); findViewById(R.id.tv_use_this_cropper_photo).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // ?????????????bitmap Bitmap croppedImage = civCropperPreview.clip(); long now = System.currentTimeMillis(); String photoId = String.format("%d.%03d", now/1000, now%1000); String fileName = photoId+".jpg"; pathCropperImage = imgCachePath + "/" + fileName; try { FileOutputStream out = new FileOutputStream(pathCropperImage); SLog.d(TAG, "try to compress file : " + pathCropperImage); // ???????60%??; if (croppedImage.compress(Bitmap.CompressFormat.JPEG, 60, out)){ out.flush(); } else{ ImageUtil.SaveBitmap(croppedImage, pathCropperImage); SLog.d(TAG, "try to save file : " + pathCropperImage); } out.close(); } catch (FileNotFoundException e) { // for FileOutputStream; e.printStackTrace(); } catch (IOException e) { // for out.flush & out.close; e.printStackTrace(); } ImageUtil.SaveBitmap(croppedImage, pathCropperImage); // ????????; if (pathPhotoTaked != null){ new File(pathPhotoTaked).delete(); } Intent data = new Intent(); if (pathCropperImage == null){ pathCropperImage = ""; } data.putExtra("CropperPhotoPath", pathCropperImage); setResult(RESULT_OK, data); CropperActivity.this.finish(); } }); pickWay = getIntent().getStringExtra("PickWay"); imgCachePath = getIntent().getStringExtra("ImgCachePath"); if (imgCachePath==null || imgCachePath.length() <= 0){ imgCachePath = Constants.BasePhotoUrlDiskCached; } if (pickWay.equals("TAKE")){ //??????; tvCancel.setVisibility(View.GONE); tvRetakePhoto.setVisibility(View.VISIBLE); startImageCaptureActivity(); } else if (pickWay.equals("PICK")){ // ????????; tvRetakePhoto.setVisibility(View.GONE); tvCancel.setVisibility(View.VISIBLE); startImageLocalPickActivity(); } else{ finish(); } } private void startImageCaptureActivity() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); long now = System.currentTimeMillis(); String photoId = String.format("%d.%03d", now/1000, now%1000); String fileName = photoId+".jpg"; pathTmpImage = imgCachePath + "/" + fileName; intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.parse("file://" + pathTmpImage)); startActivityForResult(intent, 20000); } private void startImageLocalPickActivity(){ Intent intent = new Intent(); intent.setClass(this, LocalPhotoActivity.class); if (albumName != null && albumName.length() > 0){ intent.putExtra("albumName", albumName); } startActivityForResult(intent, 20001); overridePendingTransition(R.anim.photo_picker_activity_open, R.anim.activity_stay); } @Override public void onPause(){ super.onPause(); // ????bitmap???; if (initialImage != null && !initialImage.isRecycled()){ initialImage.recycle(); initialImage = null; } if (croppedImage != null && !croppedImage.isRecycled()){ croppedImage.recycle(); croppedImage = null; } } @Override public void onStop(){ super.onStop(); if (progressDialog != null){ progressDialog.dismiss(); } // ????bitmap???; if (initialImage != null && !initialImage.isRecycled()){ initialImage.recycle(); initialImage = null; } if (croppedImage != null && !croppedImage.isRecycled()){ croppedImage.recycle(); croppedImage = null; } } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_CANCELED){ // ????????; if (pathPhotoTaked != null){ new File(pathPhotoTaked).delete(); } setResult(RESULT_CANCELED); finish(); return; } // ????????????????????????? switch (requestCode) { case 20000: // take photo; pathPhotoTaked = pathTmpImage; break; case 20001: // local photo; albumName = data.getStringExtra("albumName"); pathTmpImage = data.getStringExtra("photoPath"); break; default: break; } tvUseThisCropperPhoto.setEnabled(false); new ExtractThumbTask(true, pathTmpImage, 1536, 2048).execute(); } /* * ????????Task; * @param imageView for scrollView; */ public class ExtractThumbTask extends AsyncTask<Object, Void, String>{ String srcImgPath=null, dstImgPath=null; int reqWidth=1536, reqHeight=2048; boolean showProgressDialog = true; Bitmap bmp = null; // ??????????; public ExtractThumbTask(boolean showProgressDialog, String srcImgPath, int width, int height){ this.showProgressDialog = showProgressDialog; this.reqWidth = width; this.reqHeight = height; this.srcImgPath = srcImgPath; } protected void onPreExecute() { // ????????????????; if (showProgressDialog){ progressDialog = new AlertDialog.Builder(CropperActivity.this) .setCancelable(false) .create(); progressDialog.setCanceledOnTouchOutside(false); progressDialog.show(); progressDialog.setContentView(R.layout.progress_dialog); } } @Override protected String doInBackground(Object... params) { long now = System.currentTimeMillis(); String photoId = String.format("%d.%03d", now/1000, now%1000); String dstFileName = photoId+".jpg"; dstImgPath = imgCachePath + "/" + dstFileName; BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // ???????????????????bitmap?null BitmapFactory.decodeFile(srcImgPath, options); int w = options.outWidth; // ???????; int h = options.outHeight; // ???????; int newW = w, newH = h; // ???????????; float ratio = 1; if (w <= h){ // ??????; ratio = Math.max((float)w/reqWidth, (float)h/reqHeight); } else{ // ??????; ratio = Math.max((float)h/reqWidth, (float)w/reqHeight); } if (ratio < 1.0f){ // ?????????????;????; // Calculate inSampleSize ratio = 1; } newW = (int) (w / ratio); newH = (int) (h / ratio); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; options.inSampleSize = 1; // ??bmp; bmp = BitmapFactory.decodeFile(srcImgPath, options); if (ratio > 1.0f){ // ??????????????????????????????; bmp = ImageUtil.zoomBitmap(bmp, newW, newH); // ??; } /* try { FileOutputStream out = new FileOutputStream(dstImgPath); SLog.d(TAG, "try to compress file : " + dstImgPath); // ???????60%??; if (bmp.compress(Bitmap.CompressFormat.JPEG, 60, out)){ out.flush(); } else{ ImageUtil.SaveBitmap(bmp, dstImgPath); SLog.d(TAG, "try to save file : " + dstImgPath); } out.close(); } catch (FileNotFoundException e) { // for FileOutputStream; e.printStackTrace(); } catch (IOException e) { // for out.flush & out.close; e.printStackTrace(); } */ // bmp.recycle(); //????????; /* int rotate = ImageUtil.getPicDegree(dstImgPath); // ????????; if (rotate == 0 && options.outWidth > options.outHeight){ // ?????????????; // ????,??90?; rotate = 90; } if (rotate != 0){ Bitmap rotateBmp = ImageUtil.postRotateBitamp(bmp, rotate); bmp.recycle(); bmp = rotateBmp; } */ return dstImgPath; } @Override protected void onPostExecute(String thumbFullPath) { if (showProgressDialog){ progressDialog.dismiss(); } tvUseThisCropperPhoto.setEnabled(true); civCropperPreview.setImageBitmap(bmp); initialImage = bmp; } } }