使用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倍的样品即使相机的分辨率是很小的? –
理想情况下,您应该根据需要计算下采样因子 - 您可以首先通过解码边界(这是选项参数中的一个选项)来检查图像的维度,然后可以计算出您需要的下采样因子,然后对其进行解码真正使用这个因素。看看显示位图课程(第一个链接),它具有真正有用的示例代码。 –