1.调用系统相机拍照
package com.android.myapplication;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
private ImageView iv1;
private File imgFile;
private LinearLayout ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
iv1 = (ImageView) findViewById(R.id.iv1);
ll = (LinearLayout) findViewById(R.id.ll);
}
//通过系统拍照获取照片
public void sys_capture(View view) {
startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), 0);
}
//获取系统拍照得到的原图
public void orig_capture(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//图片名
String pic_name = "IMG_" + new SimpleDateFormat("yyyyMMdd_hhmmssSSS").format(new Date()) + ".jpg";
//图片保存路径
String pic_dir = Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator + "MyPicture";
File file = new File(pic_dir);
if(!file.exists()){
file.mkdirs();
}
//图片文件
imgFile = new File(
pic_dir+ File.separator + pic_name);
Uri imgFileUri = Uri.fromFile(imgFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imgFileUri);//指定系统拍照后图片保存的位置
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (RESULT_OK == resultCode) {
switch (requestCode) {
case 0: {
Bundle bundle = data.getExtras();//获取意图中的容器
Bitmap bitmap = (Bitmap) bundle.get("data");//此图为系统压缩后的图
iv1.setImageBitmap(bitmap);
break;
}
case 1: {
Bitmap bitmap = getFitBitMap();
ll.setBackground(new BitmapDrawable(getResources(),bitmap));
break;
}
}
}
}
/**
* 假设拍出来的照片尺寸是100x50而我显示需要的尺寸是50x40
* 则需要把原图缩小一半才能完整显示即压缩率inSampleSize的值为2
* 原图与显示图宽比值:100/50=2
* 原图与显示图高比值:50/40=1.25
* 则压缩率inSampleSize=宽高比最大的值即2
* @return
*/
private Bitmap getFitBitMap() {
//1.获取图片宽高
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;//不编解码图片只获取图片属性
BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options);
int imgWidth = options.outWidth;
int imgHeight = options.outHeight;
Log.e("===", "" + imgWidth + "---" + imgHeight);
//2.获取屏幕宽高
Display defaultDisplay = getWindowManager().getDefaultDisplay();
Point outSize = new Point();
defaultDisplay.getSize(outSize);
int screenWidth = outSize.x;
int screenHeight = outSize.y;
Log.e("===", "" + screenWidth + "---" + screenHeight);
//3.计算压缩率
int widthRatio = (int) Math.ceil(imgWidth / screenWidth);//ceil表示向上取整数即小数部分若非0则结果为整数部分加1
int heightRatio = (int) Math.ceil(imgHeight / screenHeight);
int fitInSampleSize = Math.max(widthRatio, heightRatio);
//4.根据压缩率压缩图片
options.inSampleSize = fitInSampleSize;
options.inJustDecodeBounds = false;
Bitmap fitBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options);
return fitBitmap;
}
}