Android图片裁剪Demo

代码没几行 注释写的很详细了

MainActivity

package com.example.fuck;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

//测试手机MX4
public class MainActivity extends Activity implements OnClickListener {
    public static final int PICK_REQUEST_CODE = 0X1;
    public static final int GET_REQUEST_CODE = 0X2;
    public static final int CROP_REQUEST_CODE = 0X3;
    public static final int PICTURE_REQUEST_CODE = 0x4;

    private Button button1, button2, button3;
    private ImageView image;
    private EditText editText;

    private Uri mImageUri = Uri.fromFile(new File("/sdcard/fuck.jpg"));

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        image = (ImageView) findViewById(R.id.image);
        editText = (EditText) findViewById(R.id.edittext);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            startActionPick();
            break;
        case R.id.button2:
            startActionGetContent();
            break;
        case R.id.button3:
            takePicture();
            break;

        default:
            break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Uri uri = mImageUri;
        if (data == null) {
            Log.e("fuck", 111 + "-->data:null");
        } else {
            uri = data.getData();
            Object object = data.getParcelableExtra("data");
            if (uri == null) {
                Log.e("fuck", requestCode + "-->uri:null");
            } else {
                Log.e("fuck", requestCode + "-->data:" + uri.getPath());
            }
            if (object == null) {
                Log.e("fuck", requestCode + "-->object:null");
            } else {
                Log.e("fuck", requestCode + "-->data:" + object);
            }
        }
        int width = Integer.parseInt(editText.getText().toString());
        switch (requestCode) {
        case PICK_REQUEST_CODE:
            cropImageUri(uri, width, width, CROP_REQUEST_CODE);
            break;
        case GET_REQUEST_CODE:
            cropImageUri(uri, width, width, CROP_REQUEST_CODE);
            break;
        case PICTURE_REQUEST_CODE:
            cropImageUri(uri, width, width, CROP_REQUEST_CODE);
            break;
        case CROP_REQUEST_CODE:
            image.setImageBitmap((Bitmap) data.getParcelableExtra("data"));
            break;

        default:
            break;
        }

    }

    private void startActionPick() {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        intent.putExtra("return-data", true);// 无论是否设置都不会返回bitmap对象
        startActivityForResult(intent, PICK_REQUEST_CODE);
    }

    private void startActionGetContent() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT,
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent.setType("image/*");
        intent.putExtra("return-data", true);// 无论是否设置都不会返回bitmap对象
        startActivityForResult(intent, GET_REQUEST_CODE);
    }

    private void takePicture() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);// 拍照后保存的路径
        startActivityForResult(intent, PICTURE_REQUEST_CODE);
    }

    private void cropImageUri(Uri uri, int outputX, int outputY, int requestCode) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");// 裁剪图像
        intent.putExtra("crop", "true"); // 没发现什么不同
        intent.putExtra("aspectX", 1);// 裁剪框的X比例
        intent.putExtra("aspectY", 1);// 裁剪框的Y比例

        intent.putExtra("outputX", outputX);// 输出图像的像素宽
        intent.putExtra("outputY", outputY);// 输出图像的像素高

        // 是否保持比例,当你裁剪的大小和outputx和outputy不一致是是否缩放比例
        // 如果不缩放比例,如果输出像素过小则只会显示一部分。过大同理
        intent.putExtra("scale", true);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);// 输出裁剪后的图像

        intent.putExtra("return-data", true);// 是否返回bitmap图像,(uri是我们穿进去的所以不需要返回)

        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true); // no face detection
        startActivityForResult(intent, requestCode);
    }

}
<LinearLayout 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:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="裁剪后的图片宽高:"/>

        <EditText
            android:id="@+id/edittext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number" 
            android:text="300"/>
    LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="ACTION_PICK" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="ACTION_GET_CONTENT" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="TAKE_PICTURE" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

LinearLayout>

爬虫抓我:http://blog.csdn.net/zheng5229875/article/details/48354479

你可能感兴趣的:(android)