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);
}
//Get the photo by taking a photo of the system
public void sys_capture(View view) {
startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), 0);
}
//Get the original image taken by the system
public void orig_capture(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//image name
String pic_name = "IMG_" + new SimpleDateFormat("yyyyMMdd_hhmmssSSS").format(new Date()) + ".jpg";
//image save path
String pic_dir = Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator + "MyPicture";
File file = new File(pic_dir);
if(!file.exists()){
file.mkdirs();
}
//image file
imgFile = new File(
pic_dir+ File.separator + pic_name);
Uri imgFileUri = Uri.fromFile (imgFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imgFileUri);//Specify the location where the picture is saved after the system takes a picture
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();//Get the container in the intent
Bitmap bitmap = (Bitmap) bundle.get("data");//This picture is the compressed picture of the system
iv1.setImageBitmap (bitmap);
break;
}
case 1: {
Bitmap bitmap = getFitBitMap();
ll.setBackground(new BitmapDrawable(getResources(),bitmap));
break;
}
}
}
}
/**
* Suppose the size of the photo taken is 100x50 and the size I display needs is 50x40
* You need to reduce the original image by half to display it completely, that is, the value of the compression ratio inSampleSize is 2
* The ratio of the original image to the displayed image width: 100/50=2
* The ratio of the original image to the displayed image height: 50/40=1.25
* Then the compression ratio inSampleSize=the maximum aspect ratio is 2
* @return
*/
private Bitmap getFitBitMap() {
//1. Get the image width and height
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;//Do not encode and decode pictures, only get picture properties
BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options);
int imgWidth = options.outWidth;
int imgHeight = options.outHeight;
Log.e("===", "" + imgWidth + "---" + imgHeight);
//2. Get the screen width and height
Display defaultDisplay = getWindowManager().getDefaultDisplay();
Point outSize = new Point();
defaultDisplay.getSize(outSize);
int screenWidth = outSize.x;
int screenHeight = outSize.y;
Log.e("===", "" + screenWidth + "---" + screenHeight);
//3. Calculate the compression rate
int widthRatio = (int) Math.ceil(imgWidth / screenWidth);//ceil means to round up the integer, that is, if the fractional part is not 0, the result is the integer part plus 1
int heightRatio = (int) Math.ceil(imgHeight / screenHeight);
int fitInSampleSize = Math.max(widthRatio, heightRatio);
//4. Compress the image according to the compression rate
options.inSampleSize = fitInSampleSize;
options.inJustDecodeBounds = false;
Bitmap fitBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options);
return fitBitmap;
}
}