位图在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, quality, bufOutStr);    

String outputPath = "/sdcard/Penguins2.png";
(...)
myBitmap.compress(CompressFormat.PNG, quality, 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,%20int,%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

上一篇:在jpeg中编写XMP元数据(使用PHP) – 使用单个或多个rdf:描述块


下一篇:图片是什么学习