• android 照相或从相册获取图片并裁剪


    照相或从相册获取图片并裁剪

      在android应用中很多时候都要获取图片(例如获取用户的头像)就需要从用户手机上获取图片。可以直接照,也可以从用户SD卡上获取图片,但获取到的图片未必能达到要求。所以要对图片进行一个

    裁剪才能达到要求。

      下面直接用个例子来说明怎么样来获取照相图片和相册中的图片并进行裁剪。

      对其中的一些东西进行解释:

    1.

    创建一个文件对象dir是文件保存的路径,name是文件的名称

    1 File sdcardTemple = new File(dir, name); 

    2.

    Intent intent = new Intent( "android.media.action.IMAGE_CAPTURE");

    API对android.media.action.IMAGE_CAPTURE的解释是:

    Standard Intent action that can be sent to have the camera application capture an image and return it

    就是打开照相应用获取照相的得到的图片

    3.程序的功能就是点击button后选择是照相还是从相册中获取图片,然后又结果的返回个Activity进行显示选择了哪张图片

     1 public class MainActivity extends Activity implements OnClickListener {
     2 
     3     private Button selectImageBtn;
     4     private ImageView imageView;
     5 
     6     private File sdcardTempFile;
     7     private AlertDialog dialog;
     8     private int crop = 180;
     9 
    10     @Override
    11     protected void onCreate(Bundle savedInstanceState) {
    12         super.onCreate(savedInstanceState);
    13         setContentView(R.layout.activity_main);
    14         selectImageBtn = (Button) findViewById(R.id.selectImageBtn);
    15         imageView = (ImageView) findViewById(R.id.imageView);
    16 
    17         selectImageBtn.setOnClickListener(this);
    18          //    /mnt/sdcard/是裁剪后图片保存的文件夹   如果是照相的照片是保存在/sdcard/dcim/Camera/中的
    19         sdcardTempFile = new File("/mnt/sdcard/", "tmp_pic_"
    20                 + SystemClock.currentThreadTimeMillis() + ".jpg"); 
    21     }
    22 
    23     
    24     @Override
    25     public boolean onCreateOptionsMenu(Menu menu) {
    26         getMenuInflater().inflate(R.menu.main, menu);
    27         return true;
    28     }
    29 
    30     @Override
    31     public void onClick(View v) {
    32         if (v == selectImageBtn) {
    33             if (dialog == null) {
    34                 dialog = new AlertDialog.Builder(this).setItems(
    35                         new String[] { "相机", "相册" },
    36                         new DialogInterface.OnClickListener() {
    37                             @Override
    38                             public void onClick(DialogInterface dialog,
    39                                     int which) {
    40                                 if (which == 0) {
    41                                     Intent intent = new Intent(
    42                                             "android.media.action.IMAGE_CAPTURE");
    43                                     intent.putExtra("output",
    44                                             Uri.fromFile(sdcardTempFile));
    45                                     intent.putExtra("crop", "true");
    46                                     intent.putExtra("aspectX", 1);// 裁剪框比例
    47                                     intent.putExtra("aspectY", 1);
    48                                     intent.putExtra("outputX", crop);// 输出图片大小
    49                                     intent.putExtra("outputY", crop);
    50                                     startActivityForResult(intent, 101);
    51                                 } else {
    52                                     Intent intent = new Intent(
    53                                             "android.intent.action.PICK");
    54                                     intent.setDataAndType(
    55                                             MediaStore.Images.Media.INTERNAL_CONTENT_URI,
    56                                             "image/*");
    57                                     intent.putExtra("output",
    58                                             Uri.fromFile(sdcardTempFile));
    59                                     intent.putExtra("crop", "true");
    60                                     intent.putExtra("aspectX", 1);// 裁剪框比例
    61                                     intent.putExtra("aspectY", 1);
    62                                     intent.putExtra("outputX", crop);// 输出图片大小
    63                                     intent.putExtra("outputY", crop);
    64                                     startActivityForResult(intent, 100);
    65                                 }
    66                             }
    67                         }).create();
    68             }
    69             if (!dialog.isShowing()) {
    70                 dialog.show();
    71             }
    72         }
    73     }
    74 
    75     @Override
    76     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    77         super.onActivityResult(requestCode, resultCode, data);
    78         Bitmap bmp = BitmapFactory.decodeFile(sdcardTempFile.getAbsolutePath());
    79         System.out.println(sdcardTempFile.getAbsolutePath());
    80         imageView.setImageBitmap(bmp);
    81 
    82     }
    83 
    84 }

    源码下载地址:源码

  • 相关阅读:
    设计模式(十七)Observer模式
    设计模式(十六)Mediator模式
    设计模式(十五)Facade模式
    设计模式(十四)Chain of Responsibility模式
    设计模式(十三)Visitor模式
    设计模式(十一)Composite模式
    设计模式(十二)Decorator模式
    设计模式(十)Strategy模式
    python 函数编程
    python import hashllb
  • 原文地址:https://www.cnblogs.com/liangstudyhome/p/3687774.html
Copyright © 2020-2023  润新知