Android的,COM pressing图像 [英] Android, Compressing an image

查看:78
本文介绍了Android的,COM pressing图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发送图像上经由无线或移动网络系统的网络被存储在服务器中,并再次检索。我已经做了,但由于相机拍摄它使我的应用程序缓慢的图像的大小,只是想指出,我开的画廊,并采取图片从那里,而不是直接从应用程序拍照。我注意到,从WhatsApp的图片已经从摄像机和画廊已经融为一体pressed约。 100KB。

I am sending an image over the network via wifi or the mobile network to be stored in a server and retrieved again. I've done that but due to the size of images taken by the camera it's making my app slow, just to point out I'm opening the gallery and taking the pictures from there and not taking the picture directly from the app. I have noticed that images from whatsapp that have been taken from the camera and gallery have been compressed to approx. 100kb.

目前此刻我的code需要一个文件,并将其转换成字节,然后将其发送。下面是该方法用于取得文件,并将其转换成字节。

At the moment my code takes a file and converts it to bytes and then sends it. Here is the method for taking a file and converting it to bytes.

private void toBytes(String filePath){
    try{
        File file = new File(filePath);
        InputStream is = new BufferedInputStream(new FileInputStream(file));  
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        bytes = new byte[(int) filePath.length()];
        int bytes_read;
        while((bytes_read = is.read(bytes, 0, bytes.length)) != -1){
            buffer.write(bytes, 0, bytes_read);
        }
        is.close();               
        bytes = buffer.toByteArray();
    }catch(Exception err){
        Toast.makeText(getApplicationContext(), err.toString(), Toast.LENGTH_SHORT).show();
    }
}

所以我的问题是如何将我去有关COM $ P $发送前pssing我的形象?此外,我不需要图像保持高像素数的时候,应用程序使用的图像只会占用一半的设备屏幕。

So my question is how would I go about compressing my image before sending? Also I don't need the image to retain a high pixel count as when the app uses the image it will only take up half of the devices screen.

感谢大家给予的帮助。

推荐答案

的BitMap http://developer.android.com/reference/android/graphics/Bitmap.html 类有一个 COM preSS 方法。 但是,你可能需要缩放图像 createScaledBitmap ,也可以在同一个班级。

The BitMap http://developer.android.com/reference/android/graphics/Bitmap.html class have a compress method. But you might need to scale the image createScaledBitmap, also available in the same class.

这篇关于Android的,COM pressing图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆