我如何减小图像的字节格式Android上的质量? [英] How do I reduce the quality of an image in byte format on Android?
问题描述
从使用计算器和其他有用的网站资源,我成功地创建可以上传一个Android手机上的照相机拍摄应用的图像的应用程序。唯一麻烦的是,手机我现在非常需要高质量的图像,从而导致漫长的等待,时间上传。
我也读到了从JPEG图像转换为较低的利率(更小的尺寸,或只是Web友好的大小),但code,我使用的是现在保存拍摄的图像作为一个字节(见下文code )。有什么办法来降低图像的质量,因为它是,或者我需要找到一种方法将其转换回JPEG,降低图像质量,然后将其放回字节形式的形式?
下面是code段我的工作:
如果(Intent.ACTION_SEND.equals(动作)){ 如果(extras.containsKey(Intent.EXTRA_STREAM)){
尝试{ //从被调用者的意图的资源路径
URI URI =(URI)extras.getParcelable(Intent.EXTRA_STREAM); 通过查询//陈列馆摄像机画面
//的Android ContentResolver的接口
ContentResolver的CR = getContentResolver();
InputStream为= cr.openInputStream(URI);
//获取EN code二进制字节
字节[]数据= getBytesFromFile(是); //基地64 EN code文本传输(HTTP)
INT标志= 1;
字节[] EN coded_data = Base64.en code(数据,标志);
//字节[] EN coded_data = Base64.en codeBase64(数据);
字符串image_str =新的String(EN coded_data); // 转换成
//字符串 ArrayList的<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(); nameValuePairs.add(新BasicNameValuePair(形象,
image_str)); HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(
的http://xxxxx.php);
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
HTT presponse响应= httpclient.execute(httppost);
串the_string_response = convertResponseToString(响应);
Toast.makeText(UploadImage.this,
响应+ the_string_response,
Toast.LENGTH_LONG).show();
}赶上(例外五){
Toast.makeText(UploadImage.this,ERROR+ e.getMessage()
Toast.LENGTH_LONG).show();
的System.out.println(错误http连接
+ e.toString());
}
}
}
}
有关Web应用程序,你肯定不需要这样的相机产生5+ MP图像;图像分辨率是图像大小的首要因素,所以我建议你使用BitmapFactory类生成下采样位图。
特别,看BitmapFactory.de codeByteArray的(),并把它传递一个BitmapFactory.Options参数说明你想下采样位图。
//你的位图数据
字节[] = rawBytes ..........;// downsample的因素
options.inSampleSize = 4; //下采样因子(16像素 - →1像素)//德code位与inSampleSize集
返回BitmapFactory.de codeByteArray的(rawBytes,0,rawBytes.length,期权);
有关详细信息,看一看在有效的位图显示了Android培训课程和BitmapFactory参考:
http://developer.android.com/training/displaying-bitmaps/ index.html的
http://developer.android.com/reference/android/graphics/ BitmapFactory.html
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.
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());
}
}
}
}
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.
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);
For more info, take a look at the Android Training lesson on displaying bitmaps efficiently and the reference for BitmapFactory:
http://developer.android.com/training/displaying-bitmaps/index.html
http://developer.android.com/reference/android/graphics/BitmapFactory.html
这篇关于我如何减小图像的字节格式Android上的质量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!