如何在Android上降低字节格式图像的质量?
使用StackOverflow和其他有用的网站上的资源,我成功地创建了一个应用程序,可以将摄像头应用程序拍摄的图像上传到Android手机上。唯一的问题是,我现在的手机拍摄的照片质量非常高,导致上传的等待时间很长 我读过关于将图像从jpeg转换为较低速率(较小的大小或只是网络友好的大小)的文章,但我现在使用的代码将捕获的图像保存为字节(请参见下面的代码)。是否有任何方法可以降低图像的质量,或者我是否需要找到一种方法将其转换回jpeg格式,降低图像质量,然后将其放回字节格式 下面是我正在使用的代码片段:如何在Android上降低字节格式图像的质量?,android,image,upload,compression,Android,Image,Upload,Compression,使用StackOverflow和其他有用的网站上的资源,我成功地创建了一个应用程序,可以将摄像头应用程序拍摄的图像上传到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());
}
}
}
}
if(Intent.ACTION\u SEND.equals(ACTION)){
if(额外容器(意向额外流)){
试一试{
//从被调用方获取资源路径
Uri=(Uri)extras.getParcelable(Intent.EXTRA_流);
//通过查询照相机图片库
//Android内容解析器接口
ContentResolver cr=getContentResolver();
InputStream is=cr.openInputStream(uri);
//获取用于编码的二进制字节
字节[]数据=getBytesFromFile(is);
//用于文本传输的base 64编码(HTTP)
int标志=1;
字节[]编码的_data=Base64.encode(数据、标志);
//byte[]encoded_data=Base64.encodeBase64(数据);
String image_str=新字符串(编码的_数据);//转换为
//串
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“图像”,
图像(u str);;
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(
"http://xxxxx.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
字符串\u字符串\u响应=convertResponseToString(响应);
Toast.makeText(UploadImage.this,
“响应”+字符串响应,
Toast.LENGTH_LONG).show();
}捕获(例外e){
Toast.makeText(UploadImage.this,“错误”+e.getMessage(),
Toast.LENGTH_LONG).show();
System.out.println(“http连接错误”
+e.toString());
}
}
}
}
对于web应用程序,您绝对不需要摄像头生成的5+MP图像;图像分辨率是影响图像大小的主要因素,因此我建议您使用BitmapFactory类生成下采样位图
// 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);
特别是,请查看BitmapFactory.decodeByteArray(),并向其传递一个BitmapFactory.Options参数,该参数指示您需要下采样位图
// 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参考:
要告诉解码器对图像进行二次采样,将较小的版本加载到内存中,请在BitmapFactory.Options对象中将inSampleSize设置为true。例如,分辨率为2048x1536的图像在inSampleSize为4的情况下解码后生成约为512x384的位图。将其加载到内存时,整个图像使用0.75MB而不是12MB(假设位图配置为ARGB_8888)。看到这个了吗
对于inSampleSize,即使在相机分辨率很小的情况下,这是否会继续将样本缩小4倍?理想情况下,您应该根据需要计算缩小采样系数——您可以首先通过仅解码边界来检查图像的尺寸(这是选项参数中的一个选项)然后你可以计算出你需要的下采样因子,然后用这个因子对它进行解码。请看“显示位图”课程(第一个链接),其中有非常有用的示例代码。