简体   繁体   English

图像在android中丢失质量和大小?

[英]Image losing quality and size in android?

I have an app which takes photos of receipts and upload it to a remote server.我有一个应用程序可以拍摄收据照片并将其上传到远程服务器。

I get the full-sized photo of the image from the camera intent correctly.I followed this using the official documentation in Google developer.我从相机意图中正确获得了图像的全尺寸照片。我使用 Google 开发人员中的官方文档遵循了这一点。

I then set my picture like this.然后我把我的照片设置成这样。

private void setPic() {
    // Get the dimensions of the View
    int targetW = imageView.getWidth();
    int targetH = imageView.getHeight();

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    //Bitmap bitmap = null;
    //try
    //{
        //bitmap = MediaStore.Images.Media.getBitmap(getContentResolver() , //Uri.parse(mCurrentPhotoPath));
    //}
    //catch (Exception e)
    //{
        //handle exception
    //}
    imageView.setImageBitmap(bitmap);
    new ImageSaver(getApplicationContext()).
            setFileName("currentImage.png").
            setDirectoryName("Android_Upload").
            save(bitmap);


    Model.currentImage = "currentImage.png";
}

This works fine when viewed on the device.这在设备上查看时工作正常。 But after its sent to the server and viewed from there, the image size is too small.但是在发送到服务器并从那里查看后,图像尺寸太小。

ImageSaver class pretty much saves the image elsewhere and compresses it but with 100 quality in png.I do this, so I can later the image in the database as Blob(again with 100 quality) ImageSaver 类几乎将图像保存在其他地方并对其进行压缩,但 png 质量为 100。我这样做,所以我稍后可以将数据库中的图像作为 Blob(再次具有 100 质量)

How can I decode the image and show the image in the image view but without losing quality (and the size?)如何解码图像并在图像视图中显示图像但不损失质量(和大小?)

Apparently, you have scaled down your image while decoding.显然,您在解码时缩小了图像。

You may want to remove the following line:您可能需要删除以下行:

bmOptions.inSampleSize = scaleFactor;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM