1 importandroid.content.Intent;2 importandroid.graphics.Bitmap;3 importandroid.graphics.BitmapFactory;4 importandroid.net.Uri;5 importandroid.os.Environment;6 importandroid.provider.MediaStore;7 importandroid.support.v7.app.AppCompatActivity;8 importandroid.os.Bundle;9 importandroid.util.Log;10 importandroid.view.View;11 importandroid.widget.ImageView;12 importandroid.widget.Toast;13
14 importjava.io.ByteArrayInputStream;15 importjava.io.ByteArrayOutputStream;16 importjava.io.File;17
18 public class MainActivity extends AppCompatActivity implementsView.OnClickListener {19
20 private static final int REQUESTCODE = 100,REQUESTCODE_VIDEO = 200;21 privateView take_photo;22 privateView take_video;23 privateImageView show_image;24 privateFile mImageFile;25
26 @Override27 protected voidonCreate(Bundle savedInstanceState) {28 super.onCreate(savedInstanceState);29 setContentView(R.layout.activity_main);30 show_image =(ImageView) findViewById(R.id.show_image);31 take_photo =findViewById(R.id.take_photo);32 take_video =findViewById(R.id.take_video);33
34 take_photo.setOnClickListener(this);35 take_video.setOnClickListener(this);36 }37
38 @Override39 public voidonClick(View v) {40 switch(v.getId()) {41 caseR.id.take_photo:42 Intent intent_img = newIntent(MediaStore.ACTION_IMAGE_CAPTURE);43 //创建File文件 告诉照相机 存储位置
44 mImageFile = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/" + System.currentTimeMillis() + ".jpg");45 //设置拍照朝向
46 intent_img.putExtra(MediaStore.Images.Media.ORIENTATION, 0);47 //告知意图 我们的拍照存储位置
48 intent_img.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mImageFile));49 //开启拍照
50 startActivityForResult(intent_img, REQUESTCODE);51 break;52 caseR.id.take_video:53 Intent intent_video = newIntent(MediaStore.ACTION_VIDEO_CAPTURE);54
55 File mVideoFile = new File(Environment.getExternalStorageDirectory().getAbsoluteFile()+"/"+System.currentTimeMillis()+".mp4");56 //告知意图 我们的拍照存储位置
57 intent_video.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mVideoFile));58 //设置视频拍摄时常
59 intent_video.putExtra(MediaStore.EXTRA_DURATION_LIMIT,10);60 //设置视频存储最大值
61 intent_video.putExtra(MediaStore.EXTRA_SIZE_LIMIT,1024*1024*10);62 //设置视频质量63 //intent_video.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,50);
64
65 startActivityForResult(intent_video, REQUESTCODE_VIDEO);66 break;67 }68 }69
70 @Override71 protected void onActivityResult(int requestCode, intresultCode, Intent data) {72 super.onActivityResult(requestCode, resultCode, data);73 if (REQUESTCODE == requestCode && resultCode ==RESULT_OK) {74 show_image.setImageBitmap(getimage(mImageFile.toString()));75 }76
77 /*if(REQUESTCODE_VIDEO ==requestCode && requestCode == RESULT_OK){78 Uri resultData = data.getData();79 Log.i("result uri",resultData.toString());80 Toast.makeText(this,resultData.toString(),Toast.LENGTH_SHORT).show();81 }else{82 Toast.makeText(this,"what are you 弄啥捏",Toast.LENGTH_SHORT).show();83 }*/
84 }85
86 privateBitmap getimage(String srcPath) {87 BitmapFactory.Options newOpts = newBitmapFactory.Options();88 //开始读入图片,此时把options.inJustDecodeBounds 设回true了
89 newOpts.inJustDecodeBounds = true;90 Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);//此时返回bm为空
91 newOpts.inJustDecodeBounds = false;92 int w =newOpts.outWidth;93 int h =newOpts.outHeight;94 //现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
95 float hh = 800f;//这里设置高度为800f
96 float ww = 480f;//这里设置宽度为480f97 //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
98 int be = 1;//be=1表示不缩放
99 if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
100 be = (int) (newOpts.outWidth /ww);101 } else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
102 be = (int) (newOpts.outHeight /hh);103 }104 if (be <= 0)105 be = 1;106 newOpts.inSampleSize = be;//设置缩放比例107 //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
108 bitmap =BitmapFactory.decodeFile(srcPath, newOpts);109 return compressImage(bitmap);//压缩好比例大小后再进行质量压缩
110 }111
112 privateBitmap compressImage(Bitmap image) {113 ByteArrayOutputStream baos = newByteArrayOutputStream();114 image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
115 int options = 100;116 while (baos.toByteArray().length / 1024 > 100) {//循环判断如果压缩后图片是否大于100kb,大于继续压缩
117 baos.reset();//重置baos即清空baos
118 image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
119 options -= 10;//每次都减少10
120 }121 ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
122 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
123 returnbitmap;124 }125 }