標籤:android style blog http io os 使用 java ar
作者 : 卿篤軍
原文地址:http://blog.csdn.net/qingdujun/article/details/39480117
一般相機拍攝的照片大小為3-4M左右,這裡因為需要完成將拍攝好的照片上傳到伺服器功能,所以需要將得到的照片進行壓縮。
網上搜尋了不少資料,得知可以使用:inSampleSize 設定圖片的縮放比例。
但是,這裡需要注意:
1)inJustDecodeBounds = true; 需要先設定為真,表示只獲得圖片的資料資訊。如果此時檢驗bitmap會發現bitmap==null;
2)如果需要載入圖片的時候,必須重新設定inJustDecodeBounds = false;
一、實現圖片壓縮(網上看到別人的,自己稍微修改了一下):
//壓縮圖片尺寸 public Bitmap compressBySize(String pathName, int targetWidth, int targetHeight) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true;// 不去真的解析圖片,只是擷取圖片的頭部資訊,包含寬高等; Bitmap bitmap = BitmapFactory.decodeFile(pathName, opts); // 得到圖片的寬度、高度; float imgWidth = opts.outWidth; float imgHeight = opts.outHeight; // 分別計算圖片寬度、高度與目標寬度、高度的比例;取大於等於該比例的最小整數; int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth); int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight); opts.inSampleSize = 1; if (widthRatio > 1 || widthRatio > 1) { if (widthRatio > heightRatio) { opts.inSampleSize = widthRatio; } else { opts.inSampleSize = heightRatio; } } //設定好縮放比例後,載入圖片進內容; opts.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFile(pathName, opts); return bitmap; }二、將壓縮後的圖片儲存於SD卡:
//儲存進SD卡 public void saveFile(Bitmap bm, String fileName) throws Exception { File dirFile = new File(fileName); //檢測圖片是否存在 if(dirFile.exists()){ dirFile.delete(); //刪除原圖片 } File myCaptureFile = new File(fileName); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile)); //100表示不進行壓縮,70表示壓縮率為30% bm.compress(Bitmap.CompressFormat.JPEG, 100, bos); bos.flush(); bos.close(); } 這裡注意,由於需要寫SD卡,要添加一個許可權:
<!-- 寫SD卡 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
三、附上一個完整的小Demo:
1)MainActivity.java
package com.face.sendwinrar;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends Activity {//照片儲存地址private static final String FILE_PATH = "/sdcard/gone.jpg"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { //壓縮圖片 Bitmap bitmap = compressBySize(FILE_PATH,150,200); //儲存圖片 saveFile(bitmap, FILE_PATH); } catch (Exception e) {e.printStackTrace();} } //壓縮圖片尺寸 public Bitmap compressBySize(String pathName, int targetWidth, int targetHeight) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true;// 不去真的解析圖片,只是擷取圖片的頭部資訊,包含寬高等; Bitmap bitmap = BitmapFactory.decodeFile(pathName, opts); // 得到圖片的寬度、高度; float imgWidth = opts.outWidth; float imgHeight = opts.outHeight; // 分別計算圖片寬度、高度與目標寬度、高度的比例;取大於等於該比例的最小整數; int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth); int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight); opts.inSampleSize = 1; if (widthRatio > 1 || widthRatio > 1) { if (widthRatio > heightRatio) { opts.inSampleSize = widthRatio; } else { opts.inSampleSize = heightRatio; } } //設定好縮放比例後,載入圖片進內容; opts.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFile(pathName, opts); return bitmap; } //儲存進SD卡 public void saveFile(Bitmap bm, String fileName) throws Exception { File dirFile = new File(fileName); //檢測圖片是否存在 if(dirFile.exists()){ dirFile.delete(); //刪除原圖片 } File myCaptureFile = new File(fileName); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile)); //100表示不進行壓縮,70表示壓縮率為30% bm.compress(Bitmap.CompressFormat.JPEG, 100, bos); bos.flush(); bos.close(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}2)mainfest
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dc.xust.paybyface" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <!-- 寫SD卡 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
這裡直接運行就OK 了,不需要介面,main_activity.xml檔案直接就是預設的,這裡就不附上來了。
參考文獻:幾天前的網頁,現在找不到了,找到了就添上來。
原文地址:http://blog.csdn.net/qingdujun/article/details/39480117
Android 照相機拍攝照片,壓縮後儲存於SD卡