使用StackOverflow和其他有用網站的資源,我成功地創建了一個應用程序,可以在Android手機上上傳相機應用程序拍攝的圖像。唯一的問題是,我現在使用的手機拍攝的照片質量非常高,導致漫長的等待上傳時間。如何降低Android上字節格式圖像的質量?
我讀過關於將圖像從jpeg轉換爲較低速率(較小的尺寸或只是網頁友好的尺寸),但我現在使用的代碼將捕獲的圖像保存爲一個字節(請參閱下面的代碼)。有沒有辦法以圖像的形式降低圖像的質量,或者是否需要找到一種方法將圖像轉換回jpeg,降低圖像質量,然後再以字節形式放回圖像?
這裏是代碼片段我的工作:
if (Intent.ACTION_SEND.equals(action)) {
if (extras.containsKey(Intent.EXTRA_STREAM)) {
try {
// Get resource path from intent callee
Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
// Query gallery for camera picture via
// Android ContentResolver interface
ContentResolver cr = getContentResolver();
InputStream is = cr.openInputStream(uri);
// Get binary bytes for encode
byte[] data = getBytesFromFile(is);
// base 64 encode for text transmission (HTTP)
int flags = 1;
byte[] encoded_data = Base64.encode(data, flags);
// byte[] encoded_data = Base64.encodeBase64(data);
String image_str = new String(encoded_data); // convert to
// string
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",
image_str));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://xxxxx.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String the_string_response = convertResponseToString(response);
Toast.makeText(UploadImage.this,
"Response " + the_string_response,
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(UploadImage.this, "ERROR " + e.getMessage(),
Toast.LENGTH_LONG).show();
System.out.println("Error in http connection "
+ e.toString());
}
}
}
}
對於inSampleSize,將在繼續通過縮小4倍的樣品即使相機的分辨率是很小的? –
理想情況下,您應該根據需要計算下采樣因子 - 您可以首先通過解碼邊界(這是選項參數中的一個選項)來檢查圖像的維度,然後可以計算出您需要的下采樣因子,然後對其進行解碼真正使用這個因素。看看顯示位圖課程(第一個鏈接),它具有真正有用的示例代碼。 –