实现调用相机并裁剪,调用相册并裁剪的功能

来源:互联网 发布:盈建科软件的钢号 编辑:程序博客网 时间:2024/06/14 11:45

实现一个调用相机并裁剪,调用相册并裁剪的功能。

效果图

这里写图片描述
点击更换按钮会弹出一个对话框,选择拍照还是图库

布局文件

<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="cn.edu.bzu.cn.myapp.MainActivity">    <ImageView        android:id="@+id/iv1"        android:src="@drawable/xiaoxin"        android:layout_width="100dp"        android:layout_height="100dp"        android:layout_marginTop="100dp"        android:layout_centerHorizontal="true"        />    <Button        android:text="更换头像"        android:layout_marginTop="30dp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/iv1"        android:layout_centerHorizontal="true" /></RelativeLayout>

用户交互代码

    public static final int NONE = 0;    public static final int PHOTOHRAPH = 1;// 拍照    public static final int PHOTOZOOM = 2; // 缩放    public static final int PHOTORESOULT = 3;// 结果    public static final String IMAGE_UNSPECIFIED = "image/*";    ImageView head_image_cut;    Bitmap photo;    Uri u;    File  pictureold;    String picturePath;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_head);        head_image_cut=(CircleImageView)findViewById(R.id.iv1);    }
//点击事件 public void cutPhoto(View view){        new AlertDialog.Builder(this)                .setTitle("选择来源")                .setItems(new String[]{"拍照", "图库"}, new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        switch (which) {                            case 0://拍照,调用系统相机                                Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                                intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "temp.jpg")));                                startActivityForResult(intent2, PHOTOHRAPH);                                break;                            case 1://图库,调用系统图库                                Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);                                startActivityForResult(intent, PHOTOZOOM);                                break;                        }                    }                }).show();    }

调用回调函数onActivityResult

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        if (resultCode == NONE)            return;        // 拍照        if (requestCode == PHOTOHRAPH) {            //设置文件保存路径这里放在跟目录下            pictureold = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");            startPhotoZoom(Uri.fromFile(pictureold));        }        if (requestCode == PHOTOZOOM && resultCode == RESULT_OK && null != data) {            Uri selectedImage = data.getData();//获得图片的绝对路径            String[] filePathColumn = {MediaStore.Images.Media.DATA};            Cursor cursor = getContentResolver().query(selectedImage,                    filePathColumn, null, null, null);            cursor.moveToFirst();            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);            picturePath = cursor.getString(columnIndex);            cursor.close();            pictureold=new File(picturePath);            startPhotoZoom(Uri.fromFile(pictureold));        }        // 处理结果        if (requestCode == PHOTORESOULT) {            Bundle extras = data.getExtras();            if (extras != null) {                photo = extras.getParcelable("data");                ByteArrayOutputStream stream = new ByteArrayOutputStream();                photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 - 100)压缩文件                head_image_cut.setImageBitmap(photo);//将图片放在ImageView            }        }}
//调用系统的裁剪public void startPhotoZoom(Uri uri) {        Intent intent = new Intent("com.android.camera.action.CROP");        intent.setDataAndType(uri, IMAGE_UNSPECIFIED);        intent.putExtra("crop", "true");        // aspectX aspectY 是宽高的比例        intent.putExtra("aspectX", 1);        intent.putExtra("aspectY", 1);        // outputX outputY 是裁剪图片宽高        intent.putExtra("outputX", 200);        intent.putExtra("outputY", 200);        intent.putExtra("return-data", true);        startActivityForResult(intent, PHOTORESOULT);    }
阅读全文
0 0
原创粉丝点击