首页 > 代码库 > Android 图片剪裁

Android 图片剪裁

1

public static final int REQUSET_TAKE_PHOTO = 0;
    public static final int REQUSET_AT_FRIENDS = 1;
    public static final int REQUSET_GET_PICTURE = 2;


                    Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(camera, REQUSET_TAKE_PHOTO);

                    Intent picture =
                            new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(picture, REQUSET_GET_PICTURE);

2

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUSET_GET_PICTURE && resultCode == ModifyPersionalNameActivity.RESULT_OK
                && null != data) {
            startPhotoZoom(data.getData());
           
        }
        if (requestCode == REQUSET_TAKE_PHOTO && resultCode == Activity.RESULT_OK && null != data) {
            
            String sdState = Environment.getExternalStorageState();
            if (!sdState.equals(Environment.MEDIA_MOUNTED)) {
                
                return;
            }
            new DateFormat();
            String name =
                    DateFormat.format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";
            Bundle bundle = data.getExtras();
            // 获取相机返回的数据,并转换为图片格式
            Bitmap bitmap = (Bitmap) bundle.get("data");

            Drawable drawable = new BitmapDrawable(bitmap);
            imageView_add_image1.setBackgroundDrawable(drawable);
            FileOutputStream fout = null;
            File file = new File("/sdcard/dazhima/persional_image/");
            file.mkdirs();
            String filename = file.getPath() + name;
            try {
                fout = new FileOutputStream(filename);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    fout.flush();
                    fout.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        if (requestCode == 3 && resultCode == Activity.RESULT_OK && null != data) {
            if (data != null) {
                setPicToView(data);
            }
        }
        if (requestCode == REQUSET_AT_FRIENDS && resultCode == Activity.RESULT_OK && null != data) {

            friend_name[friend_count] = data.getExtras().getString("name");
            friend_id[friend_count] = data.getExtras().getString("id");
            upload_friend[friend_count] = data.getExtras().getString("upload");
            int index = editText.getSelectionStart();
            Editable edit = editText.getEditableText();
            Log.e("==========", index + "");
            String insert = "@" + friend_name[friend_count] + " ";
            edit.insert(index, insert);
            friend_count++;
            if (ll_expression.getVisibility() == View.VISIBLE) {
                ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(this
                        .getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                ll_expression.setVisibility(View.GONE);
            }
        }
    }

public void startPhotoZoom(Uri uri) {

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        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);

            /**
             * 下面注释的方法是将裁剪之后的图片以Base64Coder的字符方式上 传到服务器,QQ头像上传采用的方法跟这个类似
             */

      /*
       * ByteArrayOutputStream stream = new ByteArrayOutputStream();
       * photo.compress(Bitmap.CompressFormat.JPEG, 60, stream); byte[] b = stream.toByteArray(); //
       * 将图片流以字符串形式存储下来
       *
       * tp = new String(Base64Coder.encodeLines(b)); 这个地方大家可以写下给服务器上传图片的实现,直接把tp直接上传就可以了,
       * 服务器处理的方法是服务器那边的事了,吼吼
       *
       * 如果下载到的服务器的数据还是以Base64Coder的形式的话,可以用以下方式转换 为我们可以用的图片类型就OK啦...吼吼 Bitmap dBitmap =
       * BitmapFactory.decodeFile(tp); Drawable drawable = new BitmapDrawable(dBitmap);
       */
            imageView_add_image1.setBackgroundDrawable(drawable);
        }
    }