標籤:
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即清空baos118 image.compress(Bitmap.CompressFormat.JPEG, options, baos);//這裡壓縮options%,把壓縮後的資料存放到baos中119 options -= 10;//每次都減少10120 }121 ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把壓縮後的資料baos存放到ByteArrayInputStream中122 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream資料產生圖片123 return bitmap;124 }125 }
其中加入了圖片壓縮,格式可以由開發人員自己定義
Android 相機與圖庫