最近有看到有朋友在讨论QQ头像的裁剪上传是怎么实现的,吼吼,小马也没做过,好奇之下学习下,发现以前项目中有类型的功能,结合官方文档里面的解释,就更好玩了,周末,急急忙忙写的,记录在博客里,希望能与大家交流学习,也恳请高手能解答小马在代码注释中提出的疑问,不管有没有人回答,小马先谢谢了,一样的,先看下效果图(效果图小马不解释了,直接流水写下去,小马是直接在模拟器里写的,能在真机上使用,因为很简单),再看代码是怎么实现的:
一:主布局界面
二:点击控件触发事件后效果图
三:拍照完之后效果图
四:裁剪界面效果图
五:点击相册后返回的图片效果图
六:裁剪完从相册PICK的保存后的效果图
下面直接来看下主控制类代码,如下:
- package com.xiaoma.piccut.demo;
-
- import java.io.File;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.drawable.BitmapDrawable;
- import android.graphics.drawable.Drawable;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Environment;
- import android.provider.MediaStore;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageButton;
- import android.widget.ImageView;
-
-
-
-
-
-
- public class PicCutDemoActivity extends Activity implements OnClickListener {
-
- private ImageButton ib = null;
- private ImageView iv = null;
- private Button btn = null;
- private String tp = null;
-
-
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- init();
- }
-
-
-
-
- private void init() {
- ib = (ImageButton) findViewById(R.id.imageButton1);
- iv = (ImageView) findViewById(R.id.imageView1);
- btn = (Button) findViewById(R.id.button1);
- ib.setOnClickListener(this);
- iv.setOnClickListener(this);
- btn.setOnClickListener(this);
- }
-
-
-
-
-
-
-
-
-
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.imageButton1:
- ShowPickDialog();
- break;
- case R.id.imageView1:
- ShowPickDialog();
- break;
- case R.id.button1:
- ShowPickDialog();
- break;
-
- default:
- break;
- }
- }
-
-
-
-
- private void ShowPickDialog() {
- new AlertDialog.Builder(this)
- .setTitle("设置头像...")
- .setNegativeButton("相册", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
-
-
-
-
- Intent intent = new Intent(Intent.ACTION_PICK, null);
-
-
-
-
-
-
-
-
- intent.setDataAndType(
- MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
- "image/*");
- startActivityForResult(intent, 1);
-
- }
- })
- .setPositiveButton("拍照", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- dialog.dismiss();
- /**
- * 下面这句还是老样子,调用快速拍照功能,至于为什么叫快速拍照,大家可以参考如下官方
- * 文档,you_sdk_path/docs/guide/topics/media/camera.html
- * 我刚看的时候因为太长就认真看,其实是错的,这个里面有用的太多了,所以大家不要认为
- * 官方文档太长了就不看了,其实是错的,这个地方小马也错了,必须改正
- */
- Intent intent = new Intent(
- MediaStore.ACTION_IMAGE_CAPTURE);
-
- intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
- .fromFile(new File(Environment
- .getExternalStorageDirectory(),
- "xiaoma.jpg")));
- startActivityForResult(intent, 2);
- }
- }).show();
- }
-
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- switch (requestCode) {
-
- case 1:
- startPhotoZoom(data.getData());
- break;
-
- case 2:
- File temp = new File(Environment.getExternalStorageDirectory()
- + "/xiaoma.jpg");
- startPhotoZoom(Uri.fromFile(temp));
- break;
-
- case 3:
-
-
-
-
-
-
-
-
- if(data != null){
- setPicToView(data);
- }
- break;
- default:
- break;
-
- }
- super.onActivityResult(requestCode, resultCode, data);
- }
-
-
-
-
-
- public void startPhotoZoom(Uri uri) {
-
-
-
-
-
-
-
- Intent intent = new Intent("com.android.camera.action.CROP");
- intent.setDataAndType(uri, "image/*");
-
- intent.putExtra("crop", "true");
-
- intent.putExtra("aspectX", 1);
- intent.putExtra("aspectY", 1);
-
- intent.putExtra("outputX", 150);
- intent.putExtra("outputY", 150);
- intent.putExtra("return-data", true);
- startActivityForResult(intent, 3);
- }
-
- /**
- * 保存裁剪之后的图片数据
- * @param picdata
- */
- private void setPicToView(Intent picdata) {
- Bundle extras = picdata.getExtras();
- if (extras != null) {
- Bitmap photo = extras.getParcelable("data");
- Drawable drawable = new BitmapDrawable(photo);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ib.setBackgroundDrawable(drawable);
- iv.setBackgroundDrawable(drawable);
- }
- }
-
- }
下面来看下裁剪中用到的类,大家详细看下头注释:
- package com.xiaoma.piccut.demo;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class Base64Coder {
-
-
- private static final String systemLineSeparator = System.getProperty("line.separator");
-
-
- private static char[] map1 = new char[64];
- static {
- int i=0;
- for (char c='A'; c<='Z'; c++) map1[i++] = c;
- for (char c='a'; c<='z'; c++) map1[i++] = c;
- for (char c='0'; c<='9'; c++) map1[i++] = c;
- map1[i++] = '+'; map1[i++] = '/'; }
-
-
- private static byte[] map2 = new byte[128];
- static {
- for (int i=0; i<map2.length; i++) map2[i] = -1;
- for (int i=0; i<64; i++) map2[map1[i]] = (byte)i; }
-
-
-
-
-
-
-
- public static String encodeString (String s) {
- return new String(encode(s.getBytes())); }
-
-
-
-
-
-
-
- public static String encodeLines (byte[] in) {
- return encodeLines(in, 0, in.length, 76, systemLineSeparator); }
-
-
-
-
-
-
-
-
-
-
- public static String encodeLines (byte[] in, int iOff, int iLen, int lineLen, String lineSeparator) {
- int blockLen = (lineLen*3) / 4;
- if (blockLen <= 0) throw new IllegalArgumentException();
- int lines = (iLen+blockLen-1) / blockLen;
- int bufLen = ((iLen+2)/3)*4 + lines*lineSeparator.length();
- StringBuilder buf = new StringBuilder(bufLen);
- int ip = 0;
- while (ip < iLen) {
- int l = Math.min(iLen-ip, blockLen);
- buf.append (encode(in, iOff+ip, l));
- buf.append (lineSeparator);
- ip += l; }
- return buf.toString(); }
-
-
-
-
-
-
-
- public static char[] encode (byte[] in) {
- return encode(in, 0, in.length); }
-
-
-
-
-
-
-
-
- public static char[] encode (byte[] in, int iLen) {
- return encode(in, 0, iLen); }
-
-
-
-
-
-
-
-
-
- public static char[] encode (byte[] in, int iOff, int iLen) {
- int oDataLen = (iLen*4+2)/3;
- int oLen = ((iLen+2)/3)*4;
- char[] out = new char[oLen];
- int ip = iOff;
- int iEnd = iOff + iLen;
- int op = 0;
- while (ip < iEnd) {
- int i0 = in[ip++] & 0xff;
- int i1 = ip < iEnd ? in[ip++] & 0xff : 0;
- int i2 = ip < iEnd ? in[ip++] & 0xff : 0;
- int o0 = i0 >>> 2;
- int o1 = ((i0 & 3) << 4) | (i1 >>> 4);
- int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
- int o3 = i2 & 0x3F;
- out[op++] = map1[o0];
- out[op++] = map1[o1];
- out[op] = op < oDataLen ? map1[o2] : '='; op++;
- out[op] = op < oDataLen ? map1[o3] : '='; op++; }
- return out; }
-
-
-
-
-
-
-
-
- public static String decodeString (String s) {
- return new String(decode(s)); }
-
-
-
-
-
-
-
-
-
- public static byte[] decodeLines (String s) {
- char[] buf = new char[s.length()+3];
- int p = 0;
- for (int ip = 0; ip < s.length(); ip++) {
- char c = s.charAt(ip);
- if (c != ' ' && c != '\r' && c != '\n' && c != '\t')
- buf[p++] = c; }
- while ((p % 4) != 0)
- buf[p++] = '0';
-
- return decode(buf, 0, p); }
-
-
-
-
-
-
-
-
- public static byte[] decode (String s) {
- return decode(s.toCharArray()); }
-
-
-
-
-
-
-
-
- public static byte[] decode (char[] in) {
- return decode(in, 0, in.length); }
-
-
-
-
-
-
-
-
-
-
- public static byte[] decode (char[] in, int iOff, int iLen) {
- if (iLen%4 != 0) throw new IllegalArgumentException ("Length of Base64 encoded input string is not a multiple of 4.");
- while (iLen > 0 && in[iOff+iLen-1] == '=') iLen--;
- int oLen = (iLen*3) / 4;
- byte[] out = new byte[oLen];
- int ip = iOff;
- int iEnd = iOff + iLen;
- int op = 0;
- while (ip < iEnd) {
- int i0 = in[ip++];
- int i1 = in[ip++];
- int i2 = ip < iEnd ? in[ip++] : 'A';
- int i3 = ip < iEnd ? in[ip++] : 'A';
- if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127)
- throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
- int b0 = map2[i0];
- int b1 = map2[i1];
- int b2 = map2[i2];
- int b3 = map2[i3];
- if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)
- throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
- int o0 = ( b0 <<2) | (b1>>>4);
- int o1 = ((b1 & 0xf)<<4) | (b2>>>2);
- int o2 = ((b2 & 3)<<6) | b3;
- out[op++] = (byte)o0;
- if (op<oLen) out[op++] = (byte)o1;
- if (op<oLen) out[op++] = (byte)o2; }
- return out; }
-
-
- private Base64Coder() {}
-
- }
最后,小马还把小DEMO源码放在附件里面,有需要的朋友可以下载下来,共同交流学习,也恳请高人回答下小马在文章注释中提出的问题,谢谢,文章是小马急急忙忙在家写的,在网吧发的,晕...不是我有多用功,这边下雨,讨厌下雨,下雨我就郁闷,来网吧玩的,顺带发下文章,吼吼,该玩的时候死命的玩,该工作的时候死命的工作,年轻时疯狂,老了不后悔,吼吼,加油加油!大家工作,也注意身体健康,嘿嘿,你懂的,不解释...哈哈