zoukankan      html  css  js  c++  java
  • Android 获取系统图库和相机照片 裁剪并显示

    接上一篇

    package com.example.image;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ImageView;
    
    public class MainActivity extends Activity implements OnClickListener {
    
    	private static final int TK_OK = 1; // 图库
    	private static final int XJ_OK = 2; // 相机
    	private static final int xj_OK_JQ = 3; // 相机拍照之后截取
    	private ImageView mImageView;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		mImageView = (ImageView) findViewById(R.id.iv);
    		findViewById(R.id.btn_xj).setOnClickListener(this);
    		findViewById(R.id.btn_tk).setOnClickListener(this);
    	}
    
    	@Override
    	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    		super.onActivityResult(requestCode, resultCode, data);
    		if (resultCode == RESULT_OK) {
    			if (requestCode == TK_OK) {
    				Bundle bundle = data.getExtras();
    				Bitmap tkPhoto = (Bitmap) bundle.get("data");
    				mImageView.setImageBitmap(tkPhoto);
    			} else if (requestCode == XJ_OK) {
    				final Bitmap xjPhoto = data.getParcelableExtra("data");
    				if (xjPhoto != null) {
    					doCropPhoto(xjPhoto);
    				}
    			} else if (requestCode == xj_OK_JQ) {
    				Bitmap xjJQPhoto = data.getParcelableExtra("data");
    				if (xjJQPhoto != null) {
    					mImageView.setImageBitmap(xjJQPhoto);
    				}
    			}
    		}
    	}
    
    	protected void doCropPhoto(Bitmap data) {
    		Intent intent = getCropImageIntent(data);
    		startActivityForResult(intent, xj_OK_JQ);
    	}
    
            /**
    	 * 用于拍照之后返回的照片裁剪
    	 * @param data
    	 * @return
    	 */
    	public static Intent getCropImageIntent(Bitmap data) {
    		Intent intent = new Intent("com.android.camera.action.CROP");
            intent.setType("image/*");
            intent.putExtra("data", data);
            intent.putExtra("crop", "true");
            intent.putExtra("aspectX", 1);
            intent.putExtra("aspectY", 1);
            intent.putExtra("outputX", 128);
            intent.putExtra("outputY", 128);
            intent.putExtra("return-data", true);
            return intent;
    	}
    
    	@Override
    	public void onClick(View v) {
    		if (v.findViewById(R.id.btn_xj) == v) {
    			Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    			startActivityForResult(intent, XJ_OK);
    		} else if (v.findViewById(R.id.btn_tk) == v) {
    			Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);    //获取系统图库并裁剪
    			intent.setType("image/*");
    			intent.putExtra("crop", "true");
    			intent.putExtra("aspectX", 1);
    			intent.putExtra("aspectY", 1);
    			intent.putExtra("outputX", 300);
    			intent.putExtra("outputY", 300);
    			intent.putExtra("scale", true);
    			intent.putExtra("return-data", true);
    			intent.putExtra("outputFormat",
    					Bitmap.CompressFormat.JPEG.toString());
    			intent.putExtra("noFaceDetection", true); // no face detection
    			startActivityForResult(intent, TK_OK);
    		}
    	}
    }
    
    
  • 相关阅读:
    CRL快速开发框架系列教程二(基于Lambda表达式查询)
    CRL快速开发框架系列教程一(Code First数据表不需再关心)
    非关系型数据库来了,CRL快速开发框架升级到版本4
    CRL快速开发框架开源完全转到Github
    火车订票系统的高并发解决方案
    用wordpress搭建个人博客
    centos/redhat安装mysql
    vue之nextTick全面解析
    微信公众号开发笔记3-sdk接入(nodejs)
    微信公众号开发笔记2(nodejs)
  • 原文地址:https://www.cnblogs.com/stareblankly/p/5051335.html
Copyright © 2011-2022 走看看