zoukankan      html  css  js  c++  java
  • 调用系统相机和图库,裁剪图片

    private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
        private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
        private static final int PHOTO_REQUEST_CUT = 3;// 结果
        private File tempFile = new File(Environment.getExternalStorageDirectory(),
                getPhotoFileName());
    
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode) {
            case PHOTO_REQUEST_TAKEPHOTO:// 当选择拍照时调用
                startPhotoZoom(Uri.fromFile(tempFile));
                break;
            case PHOTO_REQUEST_GALLERY:// 当选择从本地获取图片时
                // 做非空判断,当我们觉得不满意想重新剪裁的时候便不会报异常,下同
                if (data != null) {
                    System.out.println("11================");
                    startPhotoZoom(data.getData());
                } else {
                    System.out.println("================");
                }
                break;
            case PHOTO_REQUEST_CUT:// 返回的结果
                if (data != null)
                    // setPicToView(data);
                    sentPicToNext(data);
                break;
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
     
     
     // 使用系统当前日期加以调整作为照片的名称
        private String getPhotoFileName() {
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "'IMG'_yyyyMMdd_HHmmss");
            return dateFormat.format(date) + ".jpg";
        }
     

     

    调用系统拍照功能: 

         

     
    Intent cameraintent = new Intent(
                                        MediaStore.ACTION_IMAGE_CAPTURE);
                                // 指定调用相机拍照后照片的储存路径
                                cameraintent.putExtra(MediaStore.EXTRA_OUTPUT,
                                        Uri.fromFile(tempFile));
                                startActivityForResult(cameraintent,
                                        PHOTO_REQUEST_TAKEPHOTO);
     

     

    调用系统相册功能: 

     

    Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT);
                                getAlbum.setType("image/*");
                                startActivityForResult(getAlbum, PHOTO_REQUEST_GALLERY);

     

    调用系统裁剪功能: 

     

     
    private 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", 300);
            intent.putExtra("outputY", 300);
            intent.putExtra("return-data", true);
            intent.putExtra("noFaceDetection", true);
            System.out.println("22================");
            startActivityForResult(intent, PHOTO_REQUEST_CUT);
        }
     

     

    自定义对话框: 

     
            mDialog = new AlertDialog.Builder(this, R.style.FullScreenDialog)
                    .create();
            if (mDialog != null && !mDialog.isShowing()) {
                mDialog.show();
                mDialog.setContentView(R.layout.dialog_select_imge);
                mDialog.setCanceledOnTouchOutside(false);
    }
     

    自定义style :

     
    <style name="FullScreenDialog" parent="android:style/Theme.Dialog">
            <item name="android:windowNoTitle">true</item>
            <item name="android:windowFrame">@null</item>
            <item name="android:windowIsFloating">true</item>
            <item name="android:windowIsTranslucent">false</item>
            <item name="android:background">@android:color/transparent</item>
            <item name="android:windowBackground">@android:color/transparent</item>
            <item name="android:backgroundDimEnabled">true</item>
        </style>
  • 相关阅读:
    原代码,反码,解释和具体的补充 Java在&gt;&gt;和&gt;&gt;&gt;差异
    开源 自由 java CMS
    Socket方法LAN多线程文件传输
    《》猿从程序书评项目经理-猿自办节目
    今年,我开始在路上
    mysql 拒绝访问的解决办法
    Mysql连接错误:Mysql Host is blocked because of many connection errors
    基于jquery的从一个页面跳转到另一个页面的指定位置的实现代码
    【转】URL编码(encodeURIComponent和decodeURIComponent)
    oracle sql日期比较
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4642741.html
Copyright © 2011-2022 走看看