package com.hloong.clipheadicon; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.media.ExifInterface; import android.os.Bundle; import android.os.Environment; import android.text.TextUtils; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * 裁剪头像图片的Activity * */ public class ClipImageActivity extends Activity{ private ClipImageLayout mClipImageLayout = null; private ImageView ivBack; private TextView tvUse; public static int widthScale = 1; public static int heightScale = 1; public static final String IMAGE_FILE_NAME = "clip_temp.jpg"; public static final String RESULT_PATH = "result_path"; public static final String PASS_PATH = "pass_path"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); widthScale = getIntent().getIntExtra("widthScale",1); heightScale = getIntent().getIntExtra("heightScale",1); setContentView(R.layout.activity_clip_image); ivBack = (ImageView) findViewById(R.id.iv_back); tvUse = (TextView)findViewById(R.id.tv_use); tvUse.setOnClickListener(l); ivBack.setOnClickListener(l); mClipImageLayout = (ClipImageLayout) findViewById(R.id.clipImageLayout); String path = getIntent().getStringExtra(PASS_PATH); // 有的系统返回的图片是旋转了,有的没有旋转,所以处理 int degreee = readBitmapDegree(path); Bitmap bitmap = createBitmap(path); if (bitmap != null) { if (degreee == 0) { mClipImageLayout.setImageBitmap(bitmap); } else { mClipImageLayout.setImageBitmap(rotateBitmap(degreee, bitmap)); } } else { finish(); } } View.OnClickListener l = new View.OnClickListener() { @Override public void onClick(View v) { if(v.getId()==R.id.tv_use) { Bitmap bitmap = mClipImageLayout.clip(); String path = Environment.getExternalStorageDirectory() + File.separator+ IMAGE_FILE_NAME; saveBitmap(bitmap, path); Intent intent = new Intent(); intent.putExtra(RESULT_PATH, path); setResult(RESULT_OK, intent); finish(); } else if(v.getId()==R.id.iv_back) { finish(); } } }; private void saveBitmap(Bitmap bitmap, String path) { File f = new File(path); if (f.exists()) { f.delete(); } FileOutputStream fOut = null; try { f.createNewFile(); fOut = new FileOutputStream(f); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); fOut.flush(); } catch (IOException e1) { e1.printStackTrace(); } finally { try { if (fOut != null) fOut.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 创建图片 * * @param path * @return */ private Bitmap createBitmap(String path) { if (path == null) { return null; } BitmapFactory.Options opts = new BitmapFactory.Options(); //不在内存中读取图片的宽高 opts.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, opts); int width = opts.outWidth; opts.inSampleSize = width > 1080 ? (int)(width / 1080) : 1 ;//注意此处为了解决1080p手机拍摄图片过大所以做了一定压缩,否则bitmap会不显示 opts.inJustDecodeBounds = false;// 这里一定要将其设置回false,因为之前我们将其设置成了true opts.inPurgeable = true; opts.inInputShareable = true; opts.inDither = false; opts.inPurgeable = true; FileInputStream is = null; Bitmap bitmap = null; try { is = new FileInputStream(path); bitmap = BitmapFactory.decodeFileDescriptor(is.getFD(), null, opts); } catch (IOException e) { e.printStackTrace(); } finally { try { if (is != null) { is.close(); is = null; } } catch (IOException e) { e.printStackTrace(); } } return bitmap; } // 读取图像的旋转度 private int readBitmapDegree(String path) { int degree = 0; if(TextUtils.isEmpty(path)){ return degree; } try { ExifInterface exifInterface = new ExifInterface(path); int orientation = exifInterface.getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break; } } catch (IOException e) { e.printStackTrace(); } return degree; } // 旋转图片 private Bitmap rotateBitmap(int angle, Bitmap bitmap) { // 旋转图片 动作 Matrix matrix = new Matrix(); matrix.postRotate(angle); // 创建新的图片 Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false); return resizedBitmap; } }