简体   繁体   English

如何处理高分辨率图像?

[英]How to handle high resolution image?

I have a profile page and I also have profil image that is to be downloaded from server and then put in image view. 我有一个配置文件页面,也有要从服务器下载的配置文件图像,然后放入图像视图。 Every time I open profil page this process repeats itself. 每次我打开配置文件页面时,此过程都会重复一次。 When image is below 500 kb or around, everything is okay but when I put 6mb image on it, the page is not smoothly working. 当图像低于500 kb左右时,一切正常,但是当我在其上放置6mb图像时,页面无法正常工作。 I mean when scrolling or doing anything everything takes time. 我的意思是滚动或执行任何操作时都需要时间。 It is like running high graphic game on weak computer. 就像在弱小的计算机上运行高级图形游戏一样。 After undergoing this process, I tried to compress image and was able to reduce the size to 2 MB by this library https://github.com/zetbaitsu/Compressor . 经过此过程后,我尝试压缩图像,并通过此库https://github.com/zetbaitsu/Compressor将大小减小到2 MB。 Nothing less. 没什么。 Why does this problem occurs. 为什么会出现此问题。

I am doing this upload process on Async task. 我正在异步任务上执行此上传过程。

Upload method 上传方式

public void upload() {
    if (filePath != null) {
        Bitmap compressedImage = null;
        try {
            compressedImage = new Compressor(context).compressToBitmap(new File(filePath.getPath()));
        } catch (IOException e) {
            e.printStackTrace();
            try {
                compressedImage = MediaStore.Images.Media.getBitmap(context.getContentResolver(), filePath);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        compressedImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        final String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), compressedImage, "Title", null);

        StorageReference sRef = storageReference.child("images/" + name);
        sRef.putFile(Uri.parse(path))
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        if (ımageView != null)
                            ımageView.setImageURI(Uri.parse(filePath.getPath()));
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {
                        Log.i("resultUpload", exception.getLocalizedMessage());
                    }
                })
                .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {

                    }
                });
    } else {

    }
}

My download image method 我的下载图片方法

public void download(final ImageView ımageView) {
    StorageReference sRef = storageReference.child("images/" + name);
    sRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            Picasso.get().load(uri).into(ımageView, new Callback() {
                @Override
                public void onSuccess() {

                }

                @Override
                public void onError(Exception e) {

                }
            });
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.i("resultDownload", e.getLocalizedMessage());
        }
    });
}

What you can do, as Milan said, is to get the image from a URL online and then set that image to your imageView . 正如米兰所说,您可以做的是在线获取URL图像,然后将其设置为imageView First, you will have to make sure that you have internet permission with : 首先,您必须确保您具有以下权限:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

Then you can do the following to set an image into the imageView : 然后,您可以执行以下操作将图像设置为imageView

Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("Your image URL comes here".getContent());
imageView.setImageBitmap(bitmap); 

This should work by getting a bitmap of the image located at the URL that you have specified. 这应该通过获取位于您指定的URL上的图像的位图来实现。 It will then set the imageView to that bitmap. 然后它将imageView设置为该位图。


OR , what you can do is use Picasso. 或者 ,您可以使用毕加索。 Add this to your app/build.gradle : 将此添加到您的app/build.gradle

implementation 'com.squareup.picasso:picasso:2.5.2'

Then use this Java code to set the image into the imageView : 然后使用此Java代码将图像设置为imageView

String imageUri = "Your image URL";
Picasso.with(getApplicationContext()).load(imageUri).into(imageView);

This uses Picasso to get the image from the URL into the imageView . 这使用Picasso将图像从URL获取到imageView I hoped this helped. 我希望这会有所帮助。

This is not a Java problem. 这不是Java问题。 Irrespective of language or platform you need to do certain things to properly display an image on the web. 无论使用哪种语言或平台,都需要执行某些操作才能在网络上正确显示图像。 So, instead of recommending any Java library (I'm sure you'll get many open source libraries in every language), I'm going to enumerate the basic steps you need to follow: 因此,我不建议任何Java库(我相信您会获得每种语言的许多开源库),而是列举需要遵循的基本步骤:

  1. You need to resize (not cropping) the image for multiple devices. 您需要调整多个设备的图像大小(而不是裁剪)。 No decent website downloads 6MB image. 没有像样的网站下载6MB图像。 Never! 决不! Not even 2MB image. 甚至没有2MB的图像。 My Facebook profile image, which I captured on my 8MP mobile camera, is 160 KB in size after uploading. 上传后,我在8MP移动相机上捕获的Facebook个人资料图像大小为160 KB。 Try to bring down the image size to around 100 - 150 KB. 尝试将图像大小减小到100-150 KB左右。 These are some usual image dimensions. 这些是一些常用的图像尺寸。

在此处输入图片说明

  1. You'd create resized image for mobile, tablet and laptop. 您将为手机,平板电脑和笔记本电脑创建调整大小的图像。 For mobile, it's usually 640 x 480. For tablet, it's usually 800 x 600. For Laptop, it's usually, 1024 x 768 对于移动设备,通常为640 x480。对于平板电脑,通常为800 x600。对于笔记本电脑,通常为1024 x 768

  2. Then compress the image as you did. 然后像您一样压缩图像。 Make sure that the final image is progressive jpeg/jpg. 确保最终图像是渐进式jpeg / jpg。 Progressive jpeg is compressed in layers. 渐进式jpeg分层压缩。 The browsers that support progressive images display a hazy preview when the image is being downloaded. 下载图像时,支持渐进式图像的浏览器会显示朦胧的预览。 This improves user experience. 这样可以改善用户体验。 For the browsers that do not support progressive image, some work-arounds are used. 对于不支持渐进式图像的浏览器,使用了一些解决方法。 For eg you can create a low quality version of your image (approx. 1KB in size) with opacity: 1; filter: blur(20px); 例如,您可以创建opacity: 1; filter: blur(20px);的低质量图像版本(大小约为1KB) opacity: 1; filter: blur(20px); opacity: 1; filter: blur(20px); . Thus a blurred version of the image will be displayed as the higher quality image is being downloaded. 因此,当下载更高质量的图像时,将显示图像的模糊版本。 Initially you set the opacity of the higher quality image to 0. Once the higher quality image download is complete, you set the opacity of the lower-quality image to 0 and the higher-quality image to 1. You can test whether your image is progressive or not here and can create compressed progressive image here . 最初,您将较高质量图像的不透明度设置为0。一旦完成较高质量图像的下载,则将较低质量图像的不透明度设置为0,将较高质量图像的不透明度设置为1。您可以测试图像是否为渐进与否在这里 ,可以创建压缩的逐行扫描图像这里 You'd probably do these things programmatically, but these resources may serve as a reference. 您可能会以编程方式进行这些操作,但是这些资源可以作为参考。

  3. Ensure that your server supports HTTP/2 which is fully multiplexed. 确保您的服务器支持完全复用的HTTP / 2。 You can see the multiplexing here. 您可以在此处看到多路复用。 Thus the resources can get downloaded parallely. 因此,资源可以并行下载。

在此处输入图片说明

  1. Ensure your server supports gzip or compression. 确保您的服务器支持gzip或压缩。 Thus the resources are transferred over the network in gzipped format and extracted automatically by the browser. 因此,资源以压缩格式通过网络传输,并由浏览器自动提取。 This ensures fast download. 这样可以确保快速下载。

  2. Use proper cache-control headers, so that the browser caches the images eliminating the need to download the image for every visit. 使用适当的cache-control标头,以便浏览器缓存图像,而无需每次访问都下载图像。

  3. If possible a mobile first design should be used, so that for mobile the performance gets better. 如果可能的话,应使用移动优先设计,以便移动性能更好。

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

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