1. 引入 cropImage 工程
csdn内下载地址:http://download.csdn.net/detail/qhexin/7016925
也可以到这里下载: https://github.com/lvillani/android-cropimage
2. 加入代码
public static Bitmap getPicFromBytes(byte[] bytes, BitmapFactory.Options opts) { if (bytes != null) if (opts != null) return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,opts); else { return BitmapFactory.decodeByteArray(bytes, 0, bytes.length , null); } return null; } public static byte[] readStream(InputStream inStream) throws Exception { byte[] buffer = new byte[1024]; int len = -1; ByteArrayOutputStream outStream = new ByteArrayOutputStream(); while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray(); outStream.close(); inStream.close(); return data; } public boolean saveBitmap(Bitmap bitmap,String url) throws IOException, NumberFormatException, JSONException { File file = new File(url); FileOutputStream out; try{ int bitMapWidth = bitmap.getWidth(); int bitMapHeight = bitmap.getHeight(); int wantWidth = Integer.valueOf(m_imagePickerJson.getString("want_width")); int wantHeight = Integer.valueOf(m_imagePickerJson.getString("want_height")); Log.d("sss","Majiang bitMapWidth="+bitMapWidth + " bitMapHeight=" + bitMapHeight + " wantWidth=" + wantWidth + " wantHeight="+wantHeight ); if(wantWidth == 0) { wantWidth = bitMapWidth; wantHeight = bitMapHeight; } Bitmap bitmapCutPart = null;//切割部分 Matrix matrix=new Matrix(); if(bitMapWidth*1.0/bitMapHeight > wantWidth*1.0/wantHeight ) { int theWidthCut = (int)(wantWidth*1.0/wantHeight * bitMapHeight); matrix.postScale(wantHeight*1.0f/bitMapHeight,wantHeight*1.0f/bitMapHeight); Log.d("sss","Majiang 1111111 theWidthCut="+theWidthCut ); bitmapCutPart = Bitmap.createBitmap(bitmap , (int)(bitMapWidth/2.0f-theWidthCut/2.0f) , 0 , theWidthCut , bitMapHeight,matrix,true ); } else { matrix.postScale(wantWidth*1.0f/bitMapWidth,wantWidth*1.0f/bitMapWidth); int theHeightCut = (int)(bitMapWidth*1.0 / (wantWidth*1.0/wantHeight)); Log.d("sss","Majiang 22222 theHeightCut="+theHeightCut ); bitmapCutPart = Bitmap.createBitmap(bitmap , 0 , (int)(bitMapHeight/2- theHeightCut/2), bitMapWidth , theHeightCut,matrix,true ); } out = new FileOutputStream(file); if(bitmapCutPart.compress(Bitmap.CompressFormat.JPEG, 70, out)) { out.flush(); out.close(); } bitmapCutPart.recycle(); bitmapCutPart = null; return true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return false; } public void Local_Choose_File_Start(JSONObject prms) { m_imagePickerJson = prms; final CharSequence[] items = { "相册", "拍照" }; AlertDialog dlg = new AlertDialog.Builder(this).setTitle("选择图片").setItems(items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int item) { //这里item是根据选择的方式, 在items数组里面定义了两种方式,拍照的下标为95166所以就调用拍照方法 if(item==1){ // Intent getImageByCamera= new Intent("android.media.action.IMAGE_CAPTURE"); Uri mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "tmp_contact_" + ".jpg")); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if(mImageCaptureUri != null) { intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,mImageCaptureUri); } intent.putExtra("return-data", true); startActivityForResult(intent, 95166); }else{ Intent getImage = new Intent(Intent.ACTION_GET_CONTENT); getImage.addCategory(Intent.CATEGORY_OPENABLE); getImage.setType("image/jpeg"); startActivityForResult(getImage, 95165); } } }).create(); dlg.setOnCancelListener( new OnCancelListener(){ @Override public void onCancel(DialogInterface arg0) { // TODO Auto-generated method stub Log.d("on cancel listener", "on cancel listernner"); try { m_imagePickerJson.put("error", "1"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } AndroidNDKHelper.SendMessageWithParameters("Local_Choose_File_Result", m_imagePickerJson); m_imagePickerJson = null; } }); dlg.show(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(m_imagePickerJson != null && (requestCode == Activity_Tag_ChoosePhoto || requestCode == Activity_Tag_TakePhoto || requestCode == Activity_Tag_CropPhoto )) { if(resultCode != Activity.RESULT_OK ) { try { m_imagePickerJson.put("error", "1"); } catch (Exception e1) { e1.printStackTrace(); } AndroidNDKHelper.SendMessageWithParameters("Local_Choose_File_Result", m_imagePickerJson); m_imagePickerJson = null; return; } //选图 Bitmap myBitmap = null; byte[] mContent; ContentResolver resolver = getContentResolver(); String fileTempSave = ""; try { fileTempSave = m_imagePickerJson.getString("temp_save_image_path"); } catch (Exception e) { e.printStackTrace(); } if(requestCode ==Activity_Tag_TakePhoto ) { Uri mImageCaptureUri = Uri.fromFile(new File(fileTempSave)); Uri moriginUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "tmp_contact_" + ".jpg")); CropImageIntentBuilder cropImage = new CropImageIntentBuilder(512, 512, mImageCaptureUri); cropImage.setSourceImage(moriginUri); startActivityForResult(cropImage.getIntent(this) , Activity_Tag_CropPhoto); //startActivity(cropImage.getIntent(this) ); return; } else if( requestCode == Activity_Tag_ChoosePhoto) { Uri mImageCaptureUri = Uri.fromFile(new File(fileTempSave)); CropImageIntentBuilder cropImage = new CropImageIntentBuilder(512, 512, mImageCaptureUri); cropImage.setSourceImage(data.getData()); startActivityForResult(cropImage.getIntent(this) , Activity_Tag_CropPhoto); return; } else if (requestCode == Activity_Tag_ChoosePhoto) { try { //获得图片的uri Bundle bdl = data.getExtras(); Uri originalUri = data.getData(); if(originalUri != null) { //将图片内容解析成字节数组 mContent=readStream(resolver.openInputStream(Uri.parse(originalUri.toString()))); BitmapFactory.Options options = new BitmapFactory.Options(); //将字节数组转换为ImageView可调用的Bitmap对象 myBitmap = getPicFromBytes(mContent, null); ////把得到的图片绑定在控件上显示 //imageView.setImageBitmap(myBitmap); this.saveBitmap(myBitmap, fileTempSave);//保存为临时文件 myBitmap.recycle(); myBitmap = null; m_imagePickerJson.put("success", "1"); } else { m_imagePickerJson.put("error", "1"); } } catch (Exception e) { try { m_imagePickerJson.put("error", "1"); } catch (Exception e1) { e1.printStackTrace(); } System.out.println(e.getMessage()); } }else if(requestCode ==Activity_Tag_TakePhoto){ try { String sdStatus = Environment.getExternalStorageState(); if(sdStatus.equals(Environment.MEDIA_MOUNTED)) { try { try { Uri mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "tmp_contact_" + ".jpg")); // 将图片内容解析成字节数组 mContent = readStream(resolver.openInputStream(Uri.parse(mImageCaptureUri.toString()))); // 将字节数组转换为ImageView可调用的Bitmap对象 myBitmap = getPicFromBytes(mContent, null); //把得到的图片绑定在控件上显示 this.saveBitmap(myBitmap, fileTempSave);//保存为临时文件 myBitmap.recycle(); myBitmap = null; mContent = null; } catch (FileNotFoundException e) { e.printStackTrace(); } } catch (Exception e) { } } else { //读出来的缩略图 Bundle extras = data.getExtras(); myBitmap = (Bitmap) extras.get("data"); //把得到的图片绑定在控件上显示 //imageView.setImageBitmap(myBitmap); this.saveBitmap(myBitmap, fileTempSave);//保存为临时文件 myBitmap.recycle(); myBitmap = null; } m_imagePickerJson.put("success", "1"); } catch (Exception e) { try { m_imagePickerJson.put("error", "1"); } catch (Exception e1) { e1.printStackTrace(); } e.printStackTrace(); } } else if(requestCode == Activity_Tag_CropPhoto ) { try { //获得图片的uri // Bundle bdl = data.getExtras(); // Uri originalUri = data.getData(); // Uri mImageCaptureUri = Uri.fromFile(new File(fileTempSave)); // // if(originalUri != null) // { // //将图片内容解析成字节数组 // mContent=readStream(resolver.openInputStream(Uri.parse(originalUri.toString()))); // // BitmapFactory.Options options = new BitmapFactory.Options(); // // //将字节数组转换为ImageView可调用的Bitmap对象 // myBitmap = getPicFromBytes(mContent, null); // ////把得到的图片绑定在控件上显示 // //imageView.setImageBitmap(myBitmap); // this.saveBitmap(myBitmap, fileTempSave);//保存为临时文件 // myBitmap.recycle(); // myBitmap = null; // // m_imagePickerJson.put("success", "1"); // } // else // { // m_imagePickerJson.put("error", "1"); // } // m_imagePickerJson.put("success", "1"); } catch (Exception e) { try { m_imagePickerJson.put("error", "1"); } catch (Exception e1) { e1.printStackTrace(); } System.out.println(e.getMessage()); } AndroidNDKHelper.SendMessageWithParameters("Local_Choose_File_Result", m_imagePickerJson); m_imagePickerJson = null; } return; } if (mBalinShareManager != null) { mBalinShareManager = new BalinShareManager(this); } mBalinShareManager.addSSO(requestCode, resultCode, data); }