1 import android.content.Intent; 2 import android.graphics.Bitmap; 3 import android.graphics.BitmapFactory; 4 import android.net.Uri; 5 import android.os.Environment; 6 import android.provider.MediaStore; 7 import android.support.v7.app.AppCompatActivity; 8 import android.os.Bundle; 9 import android.util.Log; 10 import android.view.View; 11 import android.widget.ImageView; 12 import android.widget.Toast; 13 14 import java.io.ByteArrayInputStream; 15 import java.io.ByteArrayOutputStream; 16 import java.io.File; 17 18 public class MainActivity extends AppCompatActivity implements View.OnClickListener { 19 20 private static final int REQUESTCODE = 100,REQUESTCODE_VIDEO = 200; 21 private View take_photo; 22 private View take_video; 23 private ImageView show_image; 24 private File mImageFile; 25 26 @Override 27 protected void onCreate(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 @Override 39 public void onClick(View v) { 40 switch (v.getId()) { 41 case R.id.take_photo: 42 Intent intent_img = new Intent(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 case R.id.take_video: 53 Intent intent_video = new Intent(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 @Override 71 protected void onActivityResult(int requestCode, int resultCode, 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 private Bitmap getimage(String srcPath) { 87 BitmapFactory.Options newOpts = new BitmapFactory.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;//这里设置宽度为480f 97 //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 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 private Bitmap compressImage(Bitmap image) { 113 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 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 return bitmap; 124 } 125 }
其中加入了图片压缩,格式可以由开发者自己定义
时间: 2024-10-13 13:07:25