Android 如何在不影响图像质量的情况下将带有文本的图像上载到服务器,
我通过截击和位图将图像上传到服务器,并成功地传递了数据,但当我使用相机拍摄图像时,图像质量变得非常差,而且当我传递的图像大小超过500kb时,应用程序也会崩溃。为什么会发生这种情况?? 有人能帮我吗 这就是我的相机的性能Android 如何在不影响图像质量的情况下将带有文本的图像上载到服务器,,android,Android,我通过截击和位图将图像上传到服务器,并成功地传递了数据,但当我使用相机拍摄图像时,图像质量变得非常差,而且当我传递的图像大小超过500kb时,应用程序也会崩溃。为什么会发生这种情况?? 有人能帮我吗 这就是我的相机的性能 private void onCaptureImageResult(Intent data) { thumbnail = (Bitmap) data.getExtras().get("data"); File destination = new File(E
private void onCaptureImageResult(Intent data) {
thumbnail = (Bitmap) data.getExtras().get("data");
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
//fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (thumbnail!=null){
addImageNew.setImageBitmap(thumbnail);
}
}
private void onSelectFromGalleryResult(Intent data) {
thumbnail=null;
if (data != null) {
try {
thumbnail = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
if (thumbnail!=null){
addImageNew.setImageBitmap(thumbnail);
}
}
这就是我的图库意图的表现
private void onCaptureImageResult(Intent data) {
thumbnail = (Bitmap) data.getExtras().get("data");
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
//fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (thumbnail!=null){
addImageNew.setImageBitmap(thumbnail);
}
}
private void onSelectFromGalleryResult(Intent data) {
thumbnail=null;
if (data != null) {
try {
thumbnail = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
if (thumbnail!=null){
addImageNew.setImageBitmap(thumbnail);
}
}
这就是我如何将位图转换为字符串
public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 90, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
注意:我只有图像质量和高尺寸图像传递方面的问题,您可以尝试使用下面的链接解决方案。这可能对你有用
我没有找到调用
getStringImage(位图bmp)
的位置,但您可以尝试这样做:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
getStringImage(bitmap);
或者,您可以将压缩
更改为100,以获得高质量:
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
我使用getStringImage()将位图转换为传递到服务器的字符串image=getStringImage(缩略图);/**其他代码**/参数输入(按键图像,图像)//用于将映像传递到服务器