Android----调用相机、相册裁剪

-----------------------相机  裁剪-----------------------------------

public class MainActivity extends Activity{


	private ImageView imageView;
	private String path = Environment.getExternalStorageDirectory()+"/1512Q.png";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        imageView = (ImageView) findViewById(R.id.image);
        
    }
    
    //点击打开相机
    public void getdata(View v){
    	
    		//打开相机 MediaStore.ACTION_IMAGE_CAPTURE 打开相机的Action
    		Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    		//在Sdcard 中创建文件 存入图片  
    		it.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
    		//1.意图   2.requestCode 请求码
    		startActivityForResult(it, 1000);
    }
    
    //回调 重新回到Activity时 会被调用
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    	// TODO Auto-generated method stub
	    	super.onActivityResult(requestCode, resultCode, data);
	    //当拍完照以后点击完成  会执行 onActivityResult 方法 调取裁剪功能
	    	if(requestCode == 1000 && resultCode ==RESULT_OK){
	    		//调取裁剪功能  om.android.camera.action.CROP 裁剪的Action
	    		Intent it = new Intent("com.android.camera.action.CROP");
	    		//得到图片设置类型
	    		it.setDataAndType(Uri.fromFile(new File(path)), "image/*");
	    		//是否支持裁剪 设置 true 支持  false 不支持
	    		it.putExtra("CROP", true);
	    		//设置比例大小  1:1 
	    		it.putExtra("aspectX", 1);
	    		it.putExtra("aspectY", 1);
	    		//输出的大小
	    		it.putExtra("outputX", 250);
	    		it.putExtra("outputY", 250);
	    		//将裁剪好的图片进行返回到Intent中
	    		it.putExtra("return-data", true);
	    		startActivityForResult(it, 2000);
	    	}
	    	//点击完裁剪的完成以后会执行的方法
	    	if(requestCode == 2000 && resultCode == RESULT_OK){
	    		
	    		Bitmap bitmap = data.getParcelableExtra("data");
	    		imageView.setImageBitmap(bitmap);
	    		
	    		
	    		
	    	}
	    	
    
    
    }
    
    
    

}



-----------------------相册  裁剪-----------------------------------



public class MainActivity extends Activity {


	private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        imageView = (ImageView) findViewById(R.id.image);
        
    }
    
    public void getdata(View v){
    		
    		//调取系统的相册  Intent.ACTION_PICK相册
    		Intent it = new Intent(Intent.ACTION_PICK);
    		//设置格式
    		it.setType("image/*");
    		startActivityForResult(it, 1000);
    }
    
    
    	@Override
    	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    		// TODO Auto-generated method stub
    		super.onActivityResult(requestCode, resultCode, data);
   
    		
    		//得到相册里的图片进行裁剪
    		if(requestCode == 1000 && resultCode == RESULT_OK){
    			//得到相册图片
    			Uri uri = data.getData();
    			//裁剪
    			Intent it = new Intent("com.android.camera.action.CROP");
    			//设置图片 以及格式
    			it.setDataAndType(uri, "image/*");
    			//是否支持裁剪
    			it.putExtra("crop", true);
    			//设置比例
    			it.putExtra("aspectX", 1);
    			it.putExtra("aspectY", 1);
    			//设置输出的大小
    			it.putExtra("outputX", 250);
    			it.putExtra("outputY", 250);
    			//是否支持人脸识别
//    			it.putExtra("onFaceDetection", true);
    			//返回
    			it.putExtra("return-data", true);
    			startActivityForResult(it, 2000);
    		}
    	
    		//2.点击裁剪完成
    		if(requestCode == 2000 && resultCode == RESULT_OK){
    			Bitmap bitmap = data.getParcelableExtra("data");
    			imageView.setImageBitmap(bitmap);
    			
    		}
    	
    	}
    
    
}

你可能感兴趣的:(Android----调用相机、相册裁剪)