android 图片裁剪

来源:互联网 发布:云端软件平台pc 编辑:程序博客网 时间:2024/05/11 14:38
   完整代码地址: https://github.com/zhaocheng19940105/CamaerManager
private File tempFile;    protected Uri fileUri;    private ImageSelectPopWindow menu;    private static final int CROP_WIDTH_DEF = 600;       //裁剪后的图片宽度    private static final int CROP_HEIGHT_DEF = 600;      //裁剪后的图片高度    protected static final int OPEN_CAMERA_CODE = 10;// 打开相机    protected static final int OPEN_GALLERY_CODE = 11;// 打开相册    protected static final int CROP_PHOTO_CODE = 12;// 裁剪图片    private static final String IMG_PREFIX_DEFAULT = "img";    private IImageSelectedLisenter imageSelectedLisenter;    private void initFile() {        if (TextUtils.isEmpty(SDCardUtil.getPath())) {            ToastUtil.show(this, R.string.error_sd);            return;        }        String timeStamp = DateUtil.long2String(System.currentTimeMillis(), "yyyyMMdd_HHmmss");        fileUri = Uri.fromFile(FileUtil.createFile(FileConstants.getImageDirPath(this) +                getImagePrefix() + timeStamp + ".jpg"));    }    /**     * 调用相机     */    protected void openCamera() {        initFile();        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 打开相机        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);        startActivityForResult(intent, OPEN_CAMERA_CODE);    }    /**     * 打开相册     */    protected void openGallery() {//        initFile();        Intent intent = new Intent(Intent.ACTION_PICK);// 打开相册        intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*");        startActivityForResult(intent, OPEN_GALLERY_CODE);    }    /**     * 裁剪图片     *     * @param uri     * @param uri     */    protected void cropPhoto(Uri uri) {        Intent intent = new Intent("com.android.camera.action.CROP");        //检查系统intent是否可以访问        intent.setDataAndType(uri, "image/*");        //裁剪后的文件        tempFile = FileUtil.createFile(FileConstants.getImageDirPath(this) + "tmp" + System                    .currentTimeMillis() + ".jpg");        intent.putExtra("output",  Uri.fromFile(tempFile));//裁剪后的文件Uri        intent.putExtra("crop", true);        //图片裁剪的宽高比        intent.putExtra("aspectX", 1);        intent.putExtra("aspectY", 1);        Crop crop = getCrop();        if (crop != null) {            intent.putExtra("outputX", crop.getWidth());            intent.putExtra("outputY", crop.getHeight());        }        startActivityForResult(intent, CROP_PHOTO_CODE);    }    /**     * 获取图片文件     * @return     */    protected File getImageFile() {        if (tempFile == null) {            return null;        }        if (tempFile.length() > 0) {            return tempFile;        }        return null;    }    protected  Crop getCrop(){        return new Crop(CROP_WIDTH_DEF, CROP_HEIGHT_DEF);    }    /**     * 隐藏弹出菜单     */    private void dismissMenu() {        if (menu != null) {            menu.dismiss();        }    }    /**     * 弹出选择菜单     */    protected void showMenu() {        if (menu == null) {            menu = new ImageSelectPopWindow(this, menuClickLisenter);        }        // 显示窗口        menu.showAtLocation(getRootView(), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // 设置layout在PopupWindow中显示的位置    }    /***     * 弹出框菜单事件     */    private View.OnClickListener menuClickLisenter = new View.OnClickListener() {        @Override        public void onClick(View v) {            switch (v.getId()){                case R.id.btn_take_photo://拍照                    openCamera();                    break;                case R.id.btn_pick_photo://从相册选择                    openGallery();                    break;                case R.id.btn_cancel:// 取消                    break;            }            dismissMenu();        }    };    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        switch (requestCode) {            case OPEN_CAMERA_CODE:                cropPhoto(fileUri);                break;            case OPEN_GALLERY_CODE:                if (data != null) {                    cropPhoto(data.getData());                }                break;            case CROP_PHOTO_CODE:                if (null !=tempFile) {                    if (tempFile.exists() && tempFile.length() < 1) {                        tempFile.delete();                    } else {                        if (null != imageSelectedLisenter) {                            imageSelectedLisenter.onSelected(tempFile.getAbsolutePath());                        }                    }                }                try {                    //删除裁剪前的文件                    if (fileUri != null) {                        String fileName = fileUri.getEncodedPath();                        File file = new File(fileName);                        if (file != null && file.exists()) {                            file.delete();                        }                        fileUri = null;                    }                    dismissMenu();                } catch (Exception e) {                    e.printStackTrace();                }                break;            default:                break;        }    }    protected void setImageSelectedLisenter(IImageSelectedLisenter imageSelectedLisenter) {        this.imageSelectedLisenter = imageSelectedLisenter;    }    /**     * 选择处理后的图片文件名称前缀     * @return     */    protected String getImagePrefix(){        return IMG_PREFIX_DEFAULT;    }    /***     *     * @return     */    protected abstract View getRootView();    /**     * 裁剪对象模型     */    public class Crop {        private int width;        private int height;        public Crop(int width, int height) {            this.width = width;            this.height = height;        }        public int getWidth() {            return width;        }        public int getHeight() {            return height;        }    }    /***     * 弹出菜单     */    private class ImageSelectPopWindow extends PopupWindow {        private Button btn_take_photo, btn_pick_photo, btn_cancel;        private View mMenuView;        public ImageSelectPopWindow(Activity context,View.OnClickListener itemsOnClick) {            super(context);            LayoutInflater inflater = (LayoutInflater) context                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);            mMenuView = inflater.inflate(R.layout.alert_dialog, null);            btn_take_photo = (Button) mMenuView.findViewById(R.id.btn_take_photo);            btn_pick_photo = (Button) mMenuView.findViewById(R.id.btn_pick_photo);            btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);            //取消按钮            btn_cancel.setOnClickListener(new View.OnClickListener() {                public void onClick(View v) {                    //销毁弹出框                    dismiss();                }            });            //设置按钮监听            btn_pick_photo.setOnClickListener(itemsOnClick);            btn_take_photo.setOnClickListener(itemsOnClick);            //设置SelectPicPopupWindow的View            this.setContentView(mMenuView);            //设置SelectPicPopupWindow弹出窗体的宽            this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);            //设置SelectPicPopupWindow弹出窗体的高            this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);            //设置SelectPicPopupWindow弹出窗体可点击            this.setFocusable(true);            //设置SelectPicPopupWindow弹出窗体动画效果            this.setAnimationStyle(R.style.PopupAnimation);            //实例化一个ColorDrawable颜色为半透明            ColorDrawable dw = new ColorDrawable(0xb0000000);            //设置SelectPicPopupWindow弹出窗体的背景            this.setBackgroundDrawable(dw);            //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框            mMenuView.setOnTouchListener(new View.OnTouchListener() {                public boolean onTouch(View v, MotionEvent event) {                    int height = mMenuView.findViewById(R.id.pop_layout).getTop();                    int y=(int) event.getY();                    if(event.getAction()==MotionEvent.ACTION_UP){                        if(y<height){                            dismiss();                        }                    }                    return true;                }            });        }    }    /**     * 图片选择回调接口     */    public interface IImageSelectedLisenter{        /**         * 选中图片后的处理         * @param imgPath         */        public void onSelected(String imgPath);    }

0 0
原创粉丝点击