Android:提升上传到服务器的图像质量

时间:2015-10-27 11:19:54

标签: android image bitmap base64 image-compression

我试图通过将图像转换为base64字符串,将图像上传到Android应用程序中的服务器。在这种情况下,当我尝试上传从相机拍摄的图像时,我的图像质量大大降低并且非常模糊。你能帮我解决这个问题。

这是我的代码:

Intent cameraIntent = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(cameraIntent, CAMERA_REQUEST);

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent   data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {

        Log.i("camera intent", "camera intent");

        bitmap = (Bitmap) data.getExtras().get("data");

        bitmap = Bitmap.createScaledBitmap(bitmap, 720, 1280, true);

        viewImage_imageView.setImageBitmap(bitmap);
        UploadImage_textView.setEnabled(true);


ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 100, bos);
    byte[] data = bos.toByteArray();
    String file = Base64.encodeToString(data, 0);
    Log.i("base64 string", "base64 string: " + file);
    new ImageUploadTask(file).execute();
    }

}

1 个答案:

答案 0 :(得分:1)

bitmap =(Bitmap)data.getExtras()。get(" data");
这将给出相机拍摄的图像的缩略图,这是您看到模糊图像的原因

 Intent cameraIntent = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
 Uri tempURI = Uri.fromFile(<file path where you want to save image>);
 cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempURI);
 startActivityForResult(cameraIntent, CAMERA_REQUEST);

然后,对于此路径,您将获得实际的图像点击,现在将该图像上传到服务器。