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

标签 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();
    }

}

最佳答案

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

 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);

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

关于Android:提高上传到服务器的图像质量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33366617/

相关文章:

android - 在 Android 上手动从 OrientationEventListener 获取信息

android - Android 开源项目中的 Google Maps API key

javascript - 在 div 中重新定位图像

css - 在 span 中显示图像而不将其转换为 block 级元素

glTexImage2D 的 android 位图像素格式

android - Material 日期选择器自定义样式

android - 内存不足致命异常

image - 检测图像中的纹理

WPF,C# : Draw a line onto existing bitmap in image control

android - 缩放位图保持纵横比