微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

位图在Android中压缩PNG – > JPEG,反之亦然

当我从PNG转换为JPEG,然后转换为JPEG到PNG时,我的图片大小有问题.

public void onClick(View v) {
            String imageFileName = "/sdcard/Penguins2.png";
            File imageFile = new File(imageFileName);
            if (imageFile.exists()) {
                // Load the image from file
                myBitmap = BitmapFactory.decodeFile(imageFileName);
                // display the image in the image viewer
                myImageView = (ImageView) findViewById(R.id.my_image_view);
                if (myImageView != null) {
                    myImageView.setimageBitmap(myBitmap);
                }
            }
        }

转换:

private void processImage() {               
    try {
        String outputPath = "/sdcard/Penguins2.jpg";
        int quality = 100;
        FileOutputStream fileOutStr = new FileOutputStream(outputPath);
        bufferedoutputstream bufOutStr = new bufferedoutputstream(
                fileOutStr);
        myBitmap.compress(CompressFormat.JPEG,quality,bufOutStr);
        bufOutStr.flush();
        bufOutStr.close();
    } catch (FileNotFoundException exception) {
        Log.e("debug_log",exception.toString());
    } catch (IOException exception) {
        Log.e("debug_log",exception.toString());
    }
    myImageView.setimageBitmap(myBitmap);

处理完这个操作后,我只需更改这些行:

String imageFileName = "/sdcard/Penguins2.png";

String imageFileName = "/sdcard/Penguins2.jpg";

String outputPath = "/sdcard/Penguins2.jpg";
(...)
myBitmap.compress(CompressFormat.JPEG,bufOutStr);

String outputPath = "/sdcard/Penguins2.png";
(...)
myBitmap.compress(CompressFormat.PNG,bufOutStr);

图像尺寸从585847更改为531409(在DDMS中)

我想做这样的事情,因为我想使用PNG,它对于某些图像处理是无损的.
然后将图像转换为jpeg并发送为彩信,我不确定,但我认为JPEG只是MMS中所有设备支持的格式.接收器将打开图像并将其转换回png而不会丢失数据.

解决方法

除了@Sherif elKhatib答案,如果您查看文档: http://developer.android.com/reference/android/graphics/Bitmap.html#compress%28android.graphics.Bitmap.CompressFormat,%20java.io.OutputStream%29

您可以看到PNG图像不使用质量参数:

quality: Hint to the compressor,0-100. 0 meaning compress for small size,100 meaning compress for max quality. Some formats,like PNG which is lossless,will ignore the quality setting

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐