zoukankan      html  css  js  c++  java
  • Android 图片的裁剪与相机调用

    有时候我们需要的图片并不适合我们想要的大小, 那么我们就可以用到系统自带的图片裁剪功能, 把规定范围的图像给剪出来。

      贴上部分代码:

     

    1. //调用图库  
    2. Intent intent = new Intent();  
    3. intent.setType("image/*");  
    4. intent.putExtra("crop""true");    // crop=true 有这句才能出来最后的裁剪页面.  
    5. intent.putExtra("aspectX", 5);      // 这两项为裁剪框的比例.  
    6. intent.putExtra("aspectY", 4);  
    7. //输出地址  
    8. intent.putExtra("output", Uri.fromFile(new File("SDCard/1.jpg")  
    9. intent.putExtra("outputFormat""JPEG");//返回格式                        
     
    1. startActivityForResult(Intent.createChooser(intent, "选择图片"), 1); 


     

    1. //调用相机  
    2. Intent intent = new Intent(  
    3.     MediaStore.ACTION_IMAGE_CAPTURE, null);  
    4. intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(  
    5.     "SDCard/1.jpg")));  
    6. startActivityForResult(intent, 2);  


    在调用了以上任意一种方法后, 系统会返回onActivityResult, 我们在这个方法中处理就可以了

     
      1.     /** 
      2.      * 获取返回的相片 
      3.      */  
      4.     @Override  
      5.     protected void onActivityResult(int requestCode, int resultCode, Intent data)  
      6.     {  
      7.         if (resultCode == 0)  
      8.             return;  
      9.   
      10.         if (requestCode == 2)//调用系统裁剪  
      11.         {  
      12.             File picture = new File("SDCard/1.jpg");       
      13.                         startPhotoZoom(Uri.fromFile(picture));   
      14.         } else if (requestCode == PHOTO_CODE)//得到裁剪后的图片  
      15.         {  
      16.             try  
      17.             {  
      18.                 BitmapFactory.Options options = new BitmapFactory.Options();  
      19.                 options.inSampleSize = 2;  
      20.                 Bitmap bitmap = BitmapFactory.decodeFile("SDCard/1.jpg", options);  
      21.   
      22.                 if (bitmap != null)//保存图片  
      23.                 {  
      24.                     mCacheBitmap = bitmap;  
      25.   
      26.                     FileOutputStream fos = null;  
      27.                     fos = new FileOutputStream("SDCard/1.jpg");  
      28.                     mCacheBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);  
      29.                 }  
      30.   
      31.                   
      32.             } catch (Exception e)  
      33.             {  
      34.                 // TODO: handle exception  
      35.             }  
      36.         }  
      37.   
      38.         super.onActivityResult(requestCode, resultCode, data);  
      39.     }  
      40.       
      41.     /** 
      42.      * 裁剪图片 
      43.      * @param uri 
      44.      */  
      45.     public void startPhotoZoom(Uri uri) {       
      46.         Intent intent = new Intent("com.android.camera.action.CROP");       
      47.         intent.setDataAndType(uri, "image/*");  
      48.         intent.putExtra("crop""true");// crop=true 有这句才能出来最后的裁剪页面.  
      49.         intent.putExtra("aspectX"5);// 这两项为裁剪框的比例.  
      50.         intent.putExtra("aspectY"4);// x:y=1:2  
      51.         intent.putExtra("output", Uri.fromFile(new File("SDCard/1.jpg")));  
      52.         intent.putExtra("outputFormat""JPEG");//返回格式     
      53.         startActivityForResult(intent, PHOTO_CODE); 
  • 相关阅读:
    Simpo
    Numpy技巧
    流程图
    注册免费试用12个月的亚马逊AWS云计算服务
    java-Collection
    CoreGraphic
    吴裕雄--天生自然python学习笔记:python 用pygame模块开发俄罗斯方块游戏
    吴裕雄--天生自然python学习笔记:python 用pygame模块检测键盘事件和鼠标事件
    吴裕雄--天生自然python学习笔记:python 用pygame模块角色类(Sprite)移动与碰撞
    吴裕雄--天生自然python学习笔记:python 用pygame模块动画一让图片动起来
  • 原文地址:https://www.cnblogs.com/buffer/p/3231457.html
Copyright © 2011-2022 走看看