如何确保我的位图采用ARGB_8888格式且质量最高?

时间:2014-12-08 23:55:26

标签: android bitmap android-camera

我有一个相机应用程序,我的相机预览和拍摄的图像质量相当低。我一直在阅读有关位图的内容,它建议配置捕获的图像以ARGB_8888格式保存,但我读过的所有示例都没有真正符合我的用例。以下是拍摄照片时执行的功能:

@Override
    public void onPictureTaken(byte[] data, Camera camera) {
        File pictureFile = Util.getOutputMediaFile(this);
        if (pictureFile == null) {
            Toast.makeText(this, "Couldn't create file", Toast.LENGTH_SHORT)
                    .show();
        } else {
            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {
                Toast.makeText(this, "File not found exception",
                        Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                Toast.makeText(this, "IO Exception", Toast.LENGTH_SHORT).show();
            }
            BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(),
                    pictureFile.getAbsolutePath());
            Bitmap bm1 = bitmapDrawable.getBitmap();
            Matrix matrix = new Matrix();
            int w = bm1.getWidth();
            int h = bm1.getHeight();
            matrix.setScale(-1, 1);
            matrix.preRotate(-90);
            Bitmap adjustedBitmap = Bitmap.createBitmap(bm1, 0, 0,
                    w, h, matrix, true);     //I'm tempted to put it here, but it always throws errors when I try and add Bitmap.Config.ARGB_8888 as an argument to createBitmap
            OutputStream outStream = null;
            // sampleimage4 is the name given to the image saved. You can give
            // any name
            File file = new File(pictureFile.getAbsolutePath());
            try {
                outStream = new FileOutputStream(file);
                // saving as a JPEG image
                adjustedBitmap.compress(Bitmap.CompressFormat.JPEG, 100,
                        outStream);
                outStream.flush();
                outStream.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Intent intent = new Intent(this, ImagePreview.class);
            if (getIntent().getExtras() != null) {
                if (bundle != null)
                    intent.putExtra("bundle", bundle);
            }
            intent.putExtra("filePath", pictureFile.getAbsolutePath());
            intent.putExtra("load", load);
            intent.putExtra("height", preview.getHeight());
            intent.putExtra("width", preview.getWidth());

            startActivity(intent);
            camera.startPreview();
        }
    }

所以你希望能够看到我在中间评论过的那一行中使用ARGB_8888的愿望。但我不确定如何实现这一点。

0 个答案:

没有答案