Android调用手机拍照以及从相册选择照片,剪裁


/*用来标识请求照相功能的activity*/ 

    private static final int CAMERA_WITH_DATA = 3023; 

 

    /*用来标识请求gallery的activity*/ 

    private static final int PHOTO_PICKED_WITH_DATA = 3021; 

 

    /*拍照的照片存储位置*/ 

    private static final File PHOTO_DIR = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera"); 

 

    private File mCurrentPhotoFile;//照相机拍照得到的图片 

 

private void doPickPhotoAction() { 

            Context context = EditContact.this; 

         

            // Wrap our context to inflate list items using correct theme 

            final Context dialogContext = new ContextThemeWrapper(context, 

                    android.R.style.Theme_Light); 

            String cancel="返回"; 

            String[] choices; 

            choices = new String[2]; 

            choices[0] = getString(R.string.take_photo);  //拍照 

            choices[1] = getString(R.string.pick_photo);  //从相册中选择 

            final ListAdapter adapter = new ArrayAdapter<String>(dialogContext, 

                    android.R.layout.simple_list_item_1, choices); 

         

            final AlertDialog.Builder builder = new AlertDialog.Builder( 

                    dialogContext); 

            builder.setTitle(R.string.attachToContact); 

            builder.setSingleChoiceItems(adapter, -1, 

                    new DialogInterface.OnClickListener() { 

                        public void onClick(DialogInterface dialog, int which) { 

                            dialog.dismiss(); 

                            switch (which) { 

                            case 0:{ 

                                String status=Environment.getExternalStorageState(); 

                                if(status.equals(Environment.MEDIA_MOUNTED)){//判断是否有SD卡 

                                    doTakePhoto();// 用户点击了从照相机获取 

                                } 

                                else{ 

                                    showToast("没有SD卡"); 

                                } 

                                break; 

                                 

                            } 

                            case 1: 

                                doPickPhotoFromGallery();// 从相册中去获取 

                                break; 

                            } 

                        } 

                    }); 

            builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() { 

         

                @Override 

                public void onClick(DialogInterface dialog, int which) { 

                    dialog.dismiss(); 

                } 

                 

            }); 

            builder.create().show(); 

        } 

    } 

     

    /**

    * 拍照获取图片

    * 

    */ 

    protected void doTakePhoto() { 

        try { 

            // Launch camera to take photo for selected contact 

            PHOTO_DIR.mkdirs();// 创建照片的存储目录 

            mCurrentPhotoFile = new File(PHOTO_DIR, getPhotoFileName());// 给新照的照片文件命名 

            final Intent intent = getTakePickIntent(mCurrentPhotoFile); 

            startActivityForResult(intent, CAMERA_WITH_DATA); 

        } catch (ActivityNotFoundException e) { 

            Toast.makeText(this, R.string.photoPickerNotFoundText, 

                    Toast.LENGTH_LONG).show(); 

        } 

    } 

     

    public static Intent getTakePickIntent(File f) { 

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null); 

        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); 

        return intent; 

    } 

     

    /**

    * 用当前时间给取得的图片命名

    * 

    */ 

    private String getPhotoFileName() { 

        Date date = new Date(System.currentTimeMillis()); 

        SimpleDateFormat dateFormat = new SimpleDateFormat( 

                "'IMG'_yyyy-MM-dd HH:mm:ss"); 

        return dateFormat.format(date) + ".jpg"; 

    } 

     

    // 请求Gallery程序 

    protected void doPickPhotoFromGallery() { 

        try { 

            // Launch picker to choose photo for selected contact 

            final Intent intent = getPhotoPickIntent(); 

            startActivityForResult(intent, PHOTO_PICKED_WITH_DATA); 

        } catch (ActivityNotFoundException e) { 

            Toast.makeText(this, R.string.photoPickerNotFoundText1, 

                    Toast.LENGTH_LONG).show(); 

        } 

    } 

     

    // 封装请求Gallery的intent 

    public static Intent getPhotoPickIntent() { 

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); 

        intent.setType("image/*"); 

        intent.putExtra("crop", "true"); 

        intent.putExtra("aspectX", 1); 

        intent.putExtra("aspectY", 1); 

        intent.putExtra("outputX", 80); 

        intent.putExtra("outputY", 80); 

        intent.putExtra("return-data", true); 

        return intent;  

    } 

     

    // 因为调用了Camera和Gally所以要判断他们各自的返回情况,他们启动时是这样的startActivityForResult 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

        if (resultCode != RESULT_OK) 

            return; 

        switch (requestCode) { 

            case PHOTO_PICKED_WITH_DATA: {// 调用Gallery返回的 

                final Bitmap photo = data.getParcelableExtra("data"); 

                // 下面就是显示照片了 

                System.out.println(photo); 

                //缓存用户选择的图片 

                img = getBitmapByte(photo); 

                mEditor.setPhotoBitmap(photo); 

                System.out.println("set new photo"); 

                break; 

            } 

            case CAMERA_WITH_DATA: {// 照相机程序返回的,再次调用图片剪辑程序去修剪图片 

                doCropPhoto(mCurrentPhotoFile); 

                break; 

            } 

        } 

    } 

     

    protected void doCropPhoto(File f) { 

        try { 

            // 启动gallery去剪辑这个照片 

            final Intent intent = getCropImageIntent(Uri.fromFile(f)); 

            startActivityForResult(intent, PHOTO_PICKED_WITH_DATA); 

        } catch (Exception e) { 

            Toast.makeText(this, R.string.photoPickerNotFoundText, 

                    Toast.LENGTH_LONG).show(); 

        } 

    } 

     

    /** 

    * Constructs an intent for image cropping. 调用图片剪辑程序 

    */ 

    public static Intent getCropImageIntent(Uri photoUri) { 

        Intent intent = new Intent("com.android.camera.action.CROP"); 

        intent.setDataAndType(photoUri, "image/*"); 

        intent.putExtra("crop", "true"); 

        intent.putExtra("aspectX", 1); 

        intent.putExtra("aspectY", 1); 

        intent.putExtra("outputX", 80); 

        intent.putExtra("outputY", 80); 

        intent.putExtra("return-data", true); 

        return intent; 

    } 

转载于: http://wenku.baidu.com/view/6a5505a00029bd64783e2c8c.html

 ==============================================

QQ截图未命名.jpg 

2011-10-4 14:13 上传
下载附件 (14.3 KB)

当调用android手机相机的剪切图片时
/**
  * 调用android系统自带的图片裁剪,把用户选择的图片
  * 裁剪成适合用户手机的大小
  * @param imagePath
  */
public void startPhotoZoom(String imagePath) { 
  //获得裁剪图片的路径文件
  File file = new File(imagePath);
  //获得裁剪图片的名字
  originName = file.getName();
  //获得裁剪图片的uri
  Uri uri=Uri.fromFile(file);
  //调用intent跳转的裁间页面
     Intent intent = new Intent("com.android.camera.action.CROP");  
     //传入裁剪图片的路径uri和需要裁剪的格式
     intent.setDataAndType(uri, ChooseName.IMAGE_UNSPECIFIED); 
     //可裁剪状态
     intent.putExtra("crop", "true");     
     // aspectX aspectY 是宽高的比例     
     intent.putExtra("aspectX",dm.widthPixels * 2);     
     intent.putExtra("aspectY", dm.heightPixels);     
     // outputX outputY 是裁剪图片宽高     
      intent.putExtra("outputX", dm.widthPixels * 22/13);     
     intent.putExtra("outputY", dm.heightPixels * 12/13); 
     System.out.println(dm.widthPixels * 24/13);
     System.out.println(dm.heightPixels * 12/13);
        
     intent.putExtra("return-data", true);     
     startActivityForResult(intent, ChooseName.PHOTORESOULT);
}   

  intent.putExtra("outputX", dm.widthPixels*2 ); 
intent.putExtra("outputY", dm.heightPixels );    

中dm.heightPixels , dm.widthPixels*2 参数过大时就会卡刷,我裁剪的是手机屏幕大小,当每次我用屏幕分辨率高一点的手机时
就会卡死,我感觉好像是应为参数大,处理图片慢,造成的,这里好像应该用辅助线程,但又不知道怎么用,请知道的大牛帮帮小弟啊

转载于:http://www.apkbus.com/android-14534-1-1.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值