将图像从相机保存到imageView时质量下降

时间:2013-04-24 19:23:39

标签: android bitmap camera imageview png

我正在尝试创建一个应用,用户可以使用相机拍摄照片并将其保存到指定的文件夹。但是,当它从imageView转换为PNG文件时,质量会大大降低。

我认为使用这段代码可以确保质量保持高水平

bmap.compress(Bitmap.CompressFormat.PNG, 100, output);

但即使我将质量从100改为较低的数字,我也看不出任何差异。

if (requestCode == cameraData && resultCode == RESULT_OK
            && null != data) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");

        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        imageView.setImageBitmap(photo);

        // convert imageview to bitm
        imageView.buildDrawingCache();
        final Bitmap bmap = imageView.getDrawingCache();

        addIt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                OutputStream output;

                // Find the SD Card path
                File filepath = Environment.getExternalStorageDirectory();

                // Create a new folder AndroidBegin in SD Card
                File dir = new File(filepath.getAbsolutePath()
                        + "/filepath1/");
                dir.mkdirs();

                // Create a name for the saved image
                File file = new File(dir, "image.png");

                // Notify the user on successful save
                Toast.makeText(Images.this, "Image Saved to SD Card",
                        Toast.LENGTH_SHORT).show();
                try {

                    // Image starts saving
                    output = new FileOutputStream(file);

                    // Compress into png format image from 0% - 100%, using
                    // 100% for this tutorial
                    bmap.compress(Bitmap.CompressFormat.PNG, 100, output);

                    output.flush();
                    output.close();

                    Intent myIntent = new Intent(Images.this,
                            MyPreferencesActivity.class);
                    startActivity(myIntent);
                }

                // Catch exceptions
                catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

1 个答案:

答案 0 :(得分:1)

呼叫

Bitmap photo = (Bitmap) data.getExtras().get("data"); 

仅返回图像的缩略图。按照本教程保存来自摄像头的图像:http://developer.android.com/training/camera/photobasics.html