问题描述
使用StackOverflow和其他有用网站的资源,我成功地创建了一个应用程序,可以上传相机应用程序在Android手机上拍摄的图像。唯一的麻烦是,手机我现在需要非常高品质的图片,导致上传的长等待时间。
Using resources from StackOverflow and other helpful websites, I was successful in creating an application that can upload an image taken by the camera application on an Android phone. The only trouble is, the phone I have right now takes very high-quality pictures, resulting in a long wait-time for uploads.
我阅读关于从jpeg转换图像到更低的速率(更小的尺寸或只是网络友好的大小),但我现在使用的代码现在保存捕获的图像作为一个字节(见下面的代码)。有没有任何方法来降低图像的质量,它的形式,或者我需要找到一种方式将其转换回jpeg,降低图像质量,然后将其放回字节形式?
I read about converting images from jpeg to a lower rate (smaller size or just web-friendly sizes), but the code I am using right now saves the captured image as a byte (see code below). Is there any way to reduce the quality of the image in the form that it is in, or do I need to find a way to convert it back to jpeg, reduce the image quality, and then place it back in byte form?
这是我使用的代码片段:
Here is the code snippet I'm working with:
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());
}
}
}
}
推荐答案
对于网络应用程序,你绝对不需要相机产生的5+ MP图像;图像分辨率是图像大小的主要因素,因此我建议您使用BitmapFactory类来产生一个下采样的位图。
For web apps, you definitely don't need the 5+ MP images that cameras produce; image resolution is the primary factor in image size, so I'd suggest you use the BitmapFactory class to produce a downsampled bitmap.
特别是看看BitmapFactory.decodeByteArray ,并传递一个BitmapFactory.Options参数,指示您想要一个下采样位图。
Particularly, look at BitmapFactory.decodeByteArray(), and pass it a BitmapFactory.Options parameter indicating you want a downsampled bitmap.
// your bitmap data
byte[] rawBytes = .......... ;
// downsample factor
options.inSampleSize = 4; // downsample factor (16 pixels -> 1 pixel)
// Decode bitmap with inSampleSize set
return BitmapFactory.decodeByteArray(rawBytes, 0, rawBytes.length, options);
有关详情,请参阅有关显示位图的Android培训课程以及BitmapFactory :
For more info, take a look at the Android Training lesson on displaying bitmaps efficiently and the reference for BitmapFactory:
这篇关于如何在Android上以字节格式降低图像的质量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!