//拍照
public void photograph() {
// 文件夾/DCIM/CameraString path = Environment.getExternalStorageDirectory()
+ "/DCIM/Camera";
File path1 = new File(path);
if (!path1.exists()) {
path1.mkdirs();
}
File file = new File(path1, System.currentTimeMillis() + ".jpg");
avatarName = file.getAbsolutePath();
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK ) {
String sdState = Environment.getExternalStorageState();
if (!sdState.equals(Environment.MEDIA_MOUNTED)) {
Log.e("", "sd card unmount");
return;
}
try {
//首先取得屏幕對象
avatarBitmap = null;
Display display = this.getWindowManager().getDefaultDisplay();
//獲取屏幕的寬和高
int dw = display.getWidth();
int dh = display.getHeight();
/**
* 爲了計算縮放的比例,我們需要獲取整個圖片的尺寸,而不是圖片
* BitmapFactory.Options類中有一個布爾型變量inJustDecodeBounds,將其設置爲true
* 這樣,我們獲取到的就是圖片的尺寸,而不用加載圖片了。
* 當我們設置這個值的時候,我們接着就可以從BitmapFactory.Options的outWidth和outHeight中獲取到值
*/
BitmapFactory.Options op = new BitmapFactory.Options();
op.inJustDecodeBounds = true;
BitmapFactory.decodeFile(avatarName, op);//調用這個方法以後,op中的outWidth和outHeight就有值了
int wRatio = (int) Math.ceil(op.outWidth / (float) dw); //計算寬度比例
int hRatio = (int) Math.ceil(op.outHeight / (float) dh); //計算高度比例
Log.v("Width Ratio:", wRatio + "");
Log.v("Height Ratio:", hRatio + "");
/**
* 接下來,我們就需要判斷是否需要縮放以及到底對寬還是高進行縮放。
* 如果高和寬不是全都超出了屏幕,那麼無需縮放。
* 如果高和寬都超出了屏幕大小,則如何選擇縮放呢》
* 這需要判斷wRatio和hRatio的大小
* 大的一個將被縮放,因爲縮放大的時,小的應該自動進行同比率縮放。
* 縮放使用的還是inSampleSize變量
*/
if (wRatio > 1 && hRatio > 1) {
if (wRatio > hRatio) {
op.inSampleSize = wRatio;
} else {
op.inSampleSize = hRatio;
}
}
op.inJustDecodeBounds = false; //注意這裏,一定要設置爲false,因爲上面我們將其設置爲true來獲取圖片尺寸了
avatarBitmap = BitmapFactory.decodeFile(avatarName, op);
//圖片旋轉
avatarBitmap = ImageUtils.rotaingImageView(ImageUtils.readPictureDegree(avatarName), avatarBitmap);
} catch (Exception e) {
e.printStackTrace();
}
if(avatarBitmap != null){
FileOutputStream fout = null;
try {
fout = new FileOutputStream(avatarName);
avatarBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
//上傳到服務器
putAvatar();
avatarBitmap.recycle();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
fout.flush();
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 讀取圖片屬性:旋轉的角度
*
* @param path
* 圖片絕對路徑
* @return degree旋轉的角度
*/
public static int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
/**
* 圖片旋轉
* @param angle
* @param bitmap
* @return
*/
public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
// 旋轉圖片 動作
Matrix matrix = new Matrix();
matrix.postRotate(angle);
// System.out.println("angle2=" + angle);
// 創建新的圖片
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return resizedBitmap;
}