首先这些功能都是通过Intent去启动系统的服务去实现的,所以自然就有相应的Action。相关Actiong如下:

  拍照——MediaStore.ACTION_IMAGE_CAPTURE ("android.media.action.IMAGE_CAPTURE")

  相册——Intent.ACTION_GET_CONTENT("android.intent.action.GET_CONTENT" 同时要设置,intent.setType("image/*");)

"image/*");  )

  而下面表格中的参数则是在发送Intent时,添加一些额外的数据约束实现以下额外的功能

附加选项

数据类型

描述

crop

String

发送裁剪信号                               intent.putExtra("crop", "true");

aspectX

int

X方向上的比例                             intent.putExtra("aspectX", 1);

aspectY

int

Y方向上的比例                             intent.putExtra("aspectY", 1);

outputX

int

裁剪区的宽                                  intent.putExtra("outputX", 240);

outputY

int

裁剪区的高                                  intent.putExtra("outputX", 320);

scale

boolean

是否保留比例                               intent.putExtra("scale", true);

return-data

boolean

是否将数据保留在Bitmap中返回     intent.putExtra("return-data", true);

data

Parcelable

相应的Bitmap数据                       intent.putExtra("data", imgaeUri);

circleCrop

boolean

圆形裁剪区域                              intent.putExtra("circleCrop", true);

MediaStore.EXTRA_OUTPUT ("output")

URI

将URI指向相应的file:///...           intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 

outputFormat

String

输出格式                                    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());  

noFaceDetection

boolean

是否取消人脸识别功能                  intent.putExtra("noFaceDetection", true); 

 

startActivityForResult来进行Intent的发送,然后在本Activity进行接收处理。


转载地址:


public void clipPhoto(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 是宽高的比例,这里设置的是正方形(长宽比为1:1)
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 1000);
        intent.putExtra("outputY", 1000);
        //裁剪时是否保留图片的比例,这里的比例是1:1
        intent.putExtra("scale", true);
        //是否是圆形裁剪区域,设置了也不一定有效
        //intent.putExtra("circleCrop", true);
        //设置输出的格式
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        //是否将数据保留在Bitmap中返回
        intent.putExtra("return-data", true);

        startActivityForResult(intent, CUT_OK);
    }



是这样的:我使用系统的图片浏览器,然后让它自动跳到图片裁切界面,当我们定义了返回的图片大小过大,而我们实际的图片像素达不到时,系统为我们自动地填充了不够的像素成黑色,那么我们怎么样来解决这个问题呢?不说了,上代码:



Intent intent = new Intent("android.intent.action.PICK"); 
 
 
 

   intent.setDataAndType( 
 
 
 

   MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*"); 
 
 
 

   intent.putExtra("output", Uri.fromFile(sdcardTempFile)); 
 
 
 

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

   intent.putExtra("aspectX", getPicWidth>0 ? getPicWidth: DEFALUT_WIDTH);// 裁剪框比例 
 
 
 

   intent.putExtra("aspectY", getPicHeight>0 ? getPicWidth: DEFALUT_HEIGHT); 
 
 
 

   intent.putExtra("outputX", getPicWidth>0 ? getPicWidth: DEFALUT_WIDTH);// 输出图片大小 
 
 
 

   intent.putExtra("outputY", getPicHeight>0 ? getPicWidth: DEFALUT_HEIGHT); 
 
 
 

   //加上下面的这两句之后,系统就会把图片给我们拉伸了,哇哈哈,愁死我例差点 
 
 
 
 
  intent.putExtra("scale",true); 
 
 
 
 
  intent.putExtra("scaleUpIfNeeded", true); 
 
 
 

   context.startActivityForResult(intent, RCODE_FROM_ALBUME);



    1. /**
    2.  * 调用系统相册
    3.  */  
    4. private void requestGallery() {  
    5. new Intent(Intent.ACTION_PICK);  
    6. "image/*");  
    7. "crop", "true");  
    8. "noFaceDetection", true);  
    9. "scale", true);  
    10. "scaleUpIfNeeded", true);  
    11. "aspectX", 1);  
    12. "aspectY", 1);  
    13. "outputX", 120);  
    14. "outputY", 120);  
    15. "return-data", true);  
    16.   
    17.     startActivityForResult(intent, SELECT_PICTURE);  
    18. }  
    19.   
    20. /**  
    21.  * 调用系统拍照  
    22.  */  
    23. private void requestCapture() {  
    24. new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
    25.   
    26. "noFaceDetection", true);  
    27. "scale", true);  
    28. "scaleUpIfNeeded", true);  
    29. "aspectX", 1);  
    30. "aspectY", 1);  
    31. "outputX", 120);  
    32. "outputY", 120);  
    33. "return-data", true);  
    34.   
    35. new File(getCameraPath());  
    36. if (!file.getParentFile().exists())  
    37.         file.getParentFile().mkdirs();  
    38.     Uri picUri = Uri.fromFile(file);</span>  
    39.     intent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);  
    40.   
    41.     startActivityForResult(intent, CROP_FROM_CAMERA);  
    42. }





    一个完整例子源码:


    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="takeC"
            android:text="拍照" />
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="getP"
            android:text="从相册选择" />
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="260dip"
                android:scaleType="centerCrop" />
        </FrameLayout>
    
    </LinearLayout>



    public class MainActivity extends AppCompatActivity {
    
        private ImageView image;
        protected static final int CHOOSE_PICTURE = 0;
        protected static final int TAKE_PICTURE = 1;
        private static final int CROP_SMALL_PICTURE = 2;
        protected static Uri tempUri;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            image = (ImageView) findViewById(R.id.imageView);
            WindowManager wm = this.getWindowManager();
    
            int width = wm.getDefaultDisplay().getWidth();
            FrameLayout.LayoutParams lp1= (FrameLayout.LayoutParams) image.getLayoutParams();
            lp1.height= (int) (width*9.0/16f);
        }
    
        //拍照
        public void takeC(View view) {
            Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            tempUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "image.jpg"));
            // 指定照片保存路径(SD卡),image.jpg为一个临时文件,每次拍照后这个图片都会被替换
            openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
            startActivityForResult(openCameraIntent, TAKE_PICTURE);
        }
    
        //从相册获取照片
        public void getP(View view) {
            Intent openAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT);
            openAlbumIntent.setType("image/*");
            startActivityForResult(openAlbumIntent, CHOOSE_PICTURE);
        }
    
        //回调函数
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK) { // 如果返回码是可以用的
                switch (requestCode) {
                    case TAKE_PICTURE:
                        startPhotoZoom(tempUri); // 开始对图片进行裁剪处理
                        break;
                    case CHOOSE_PICTURE:
                        startPhotoZoom(data.getData()); // 开始对图片进行裁剪处理
                        break;
                    case CROP_SMALL_PICTURE:
                        if (data != null) {
                            setImageToView(data); // 让刚才选择裁剪得到的图片显示在界面上
                        }
                        break;
                }
            }
        }
    
        /**
         * 裁剪图片方法实现
         *
         * @param uri
         */
        protected void startPhotoZoom(Uri uri) {
            if (uri == null) {
                Log.i("tag", "The uri is not exist.");
            }
            tempUri = uri;
            Intent intent = new Intent("com.android.camera.action.CROP");
            intent.setDataAndType(uri, "image/*");
            // 设置裁剪
            intent.putExtra("crop", "true");
            // aspectX aspectY 是宽高的比例
            intent.putExtra("aspectX", 16);
            intent.putExtra("aspectY", 9);
            // outputX outputY 是裁剪图片宽高
            intent.putExtra("outputX", 640);
            intent.putExtra("outputY", 320);
            intent.putExtra("scale", true);
            intent.putExtra("scaleUpIfNeeded", true);
            intent.putExtra("return-data", true);
            startActivityForResult(intent, CROP_SMALL_PICTURE);
        }
    
        /**
         * 保存裁剪之后的图片数据
         *
         * @param
         * @param data
         */
        protected void setImageToView(Intent data) {
            Bundle extras = data.getExtras();
            if (extras != null) {
                Bitmap photo = extras.getParcelable("data");
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                photo.compress(Bitmap.CompressFormat.PNG, 100, baos);
                byte[] bytes=baos.toByteArray();
    
                Glide.with(this).load(bytes).into(image);
            }
        }
    }



    权限:


    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


    还有的是获取原图片,不经裁剪




    public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == REQUEST_IMAGE_CAMERA && resultCode == Activity.RESULT_OK) {
    //            startPhotoZoom(uri); // 开始对图片进行裁剪处理
                //回调函数,获取拍照图片的路径
                String path = uri.getPath();
                //通过路径找到文件
                File outputFile = new File(path);
                //将文件添加到addPicture函数中
                addPicture(outputFile);
                mFiles.add(outputFile);
            } else if (requestCode == REQUEST_IMAGE_ALBUM && resultCode == Activity.RESULT_OK) {
                if (data != null && data.getData() != null) {
                    //获取到相册图片的uri
                    Uri uri = data.getData();
    //                startPhotoZoom(uri); // 开始对图片进行裁剪处理
                    //获取图片路径
                    String[] proj = {MediaStore.Images.Media.DATA};
                    //通过uri,在proj中进行游标搜索
                    Cursor cursor = getContentResolver().query(uri, proj, null, null, null);
                    //找到图片的索引值
                    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    //将游标移至到首位,为下次使用做准备
                    cursor.moveToFirst();
                    //根据索引地址,找到图片的路径
                    String path = cursor.getString(column_index);
                    //根据路径找到图片
                    File imageFile = new File(path);
                    //将图片添加到addPicture函数中
                    addPicture(imageFile);
    
                }
            }

    下面乱糟糟的代码片我也不知道是什么,可能是格式遗留的东西,不管他吧。。。。

    还是好难看,怎么删啊!!!