Android ------ 拍照相册选择图片 剪裁图片

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="16dp"

    android:paddingLeft="16dp"

    android:paddingRight="16dp"

    android:paddingTop="16dp"

    tools:context=".MainActivity" >



    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true"

        android:layout_marginLeft="38dp"

        android:layout_marginTop="46dp"

        android:text="Button" />



    <ImageView

        android:id="@+id/imageView"

        android:layout_width="300dp"

        android:layout_height="300dp"

        android:layout_below="@+id/button1"

        android:layout_centerHorizontal="true" />



</RelativeLayout>

 

public class MainActivity extends Activity implements View.OnClickListener {

     private Button button1;

     private ImageView imageView;

     private File tempFile = new File(Environment.getExternalStorageDirectory(),

             getPhotoFileName());

     private File sdcardTempFile;

     private AlertDialog dialog;

     private int crop = 300;

     //private static final int PHOTO_REQUEST_CUT = 3;// 结果



    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        // 显示图片

        imageView = (ImageView) findViewById(R.id.imageView);

        

        // 点击按钮,选择图片或者拍照

        button1 = (Button) findViewById(R.id.button1);

        button1.setOnClickListener((OnClickListener) this);

        

        sdcardTempFile = new File("/mnt/sdcard/", "tmp_pic_"

                + SystemClock.currentThreadTimeMillis() + ".jpg");



    }



    @Override

    public void onClick(View v) {

        if (v == button1) {

            if (dialog == null) {

                dialog = new AlertDialog.Builder(this).setItems(

                        new String[] { "相机", "相册" },

                        new DialogInterface.OnClickListener() {

                            @Override

                            public void onClick(DialogInterface dialog,

                                    int which) {

                                if (which == 0) {

                                    // 选择拍照

                                    Intent cameraintent = new Intent(

                                            MediaStore.ACTION_IMAGE_CAPTURE);

                                    // 指定调用相机拍照后照片的储存路径

                                    cameraintent.putExtra(

                                            MediaStore.EXTRA_OUTPUT,

                                            Uri.fromFile(tempFile));

                                    startActivityForResult(cameraintent, 101);



                                } else {

                                    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", 1);// 裁剪框比例

                                    intent.putExtra("aspectY", 1);

                                    intent.putExtra("outputX", crop);// 输出图片大小

                                    intent.putExtra("outputY", crop);

                                    startActivityForResult(intent, 100);

                                }

                            }

                        }).create();

            }

            if (!dialog.isShowing()) {

                dialog.show();

            }

        }

    }



    @Override

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

        if (requestCode == 100) {

            Bitmap bmp = BitmapFactory.decodeFile(sdcardTempFile

                    .getAbsolutePath());

            imageView.setImageBitmap(bmp);

        } else if (requestCode == 101) {

            // 选择拍照

            startPhotoZoom(Uri.fromFile(tempFile));

        } else if (requestCode == 102) {

            if (data != null)

                // setPicToView(data);

                sentPicToNext(data);



        }

    }

     

     

    private void startPhotoZoom(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 是宽高的比例

        intent.putExtra("aspectX", 1);

        intent.putExtra("aspectY", 1);



        // outputX,outputY 是剪裁图片的宽高

        intent.putExtra("outputX", 300);

        intent.putExtra("outputY", 300);

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

        intent.putExtra("noFaceDetection", true);

        startActivityForResult(intent, 102);



    }



    // 将进行剪裁后的图片传递到下一个界面上

    private void sentPicToNext(Intent picdata) {

        Bundle bundle = picdata.getExtras();

        if (bundle != null) {

            Bitmap photo = bundle.getParcelable("data");

            if (photo == null) {

            } else {

                imageView.setImageBitmap(photo);

            }



            ByteArrayOutputStream baos = null;

            try {

                baos = new ByteArrayOutputStream();

                // 压缩图片

                photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);

                byte[] photodata = baos.toByteArray();

                System.out.println(photodata.toString());

                // Intent intent = new Intent();

                // intent.setClass(RegisterActivity.this, ShowActivity.class);

                // intent.putExtra("photo", photodata);

                // startActivity(intent);

                // finish();

            } catch (Exception e) {

                e.getStackTrace();

            } finally {

                if (baos != null) {

                    try {

                        baos.close();

                    } catch (Exception e) {

                        e.printStackTrace();

                    }

                }

            }

        }

    }



    // 使用系统当前日期加以调整作为照片的名称

    private String getPhotoFileName() {

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

        SimpleDateFormat dateFormat = new SimpleDateFormat(

                "'IMG'_yyyyMMdd_HHmmss");

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

    }



}

 

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



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

 

 

你可能感兴趣的:(android)