自定义相机的画质不佳

时间:2014-09-18 10:13:16

标签: android image camera

目前我正在使用自定义相机应用程序,预览看起来很好。但是当我拍摄照片并在我的其他活动中显示时,图片已经减少了80%。谁知道为什么会这样?同样在画廊,质量很差。 我正在使用android中的Camera API演示。

我的参数:

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    parameters = mCamera.getParameters();


    parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
    parameters.setFlashMode(Parameters.FLASH_MODE_AUTO);
    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
    parameters.setJpegQuality(100);
    parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
    parameters.setRotation(90);
    Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    if (display.getRotation() == Surface.ROTATION_0) {
        mCamera.setDisplayOrientation(90);
    } else if (display.getRotation() == Surface.ROTATION_270) {
        mCamera.setDisplayOrientation(180);
    }

    mCamera.setParameters(parameters);

    mCamera.startPreview();
}

代码保存图片:

private PictureCallback mPicture = new PictureCallback() {
    private String TAG = "DocsPro";

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null) {
            Log.d(TAG, "Error creating media file, check storage permissions : PICTURE FILE IS NULL");
            return;
        }

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
            String filepath = pictureFile.getAbsolutePath();
            Intent edit = new Intent(TakePhoto.this, EditPhoto.class);
            edit.putExtra("filepath", filepath);
            startActivity(edit);
            finish();
        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, "Error accessing file: " + e.getMessage());
        }
    }
};

private static File getOutputMediaFile(int type) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "DocsPro");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "IMG_" + timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}

1 个答案:

答案 0 :(得分:6)

解决问题: 我只是把它放在预览课上改变的表面。

 List<Camera.Size> sizes = parameters.getSupportedPictureSizes();
    Camera.Size size = sizes.get(0);
    for(int i=0;i<sizes.size();i++)
    {
        if(sizes.get(i).width > size.width)
            size = sizes.get(i);
    }
    parameters.setPictureSize(size.width, size.height);

然后只调整位图大小,因为图片很大:

    imageView = (ImageView) findViewById(R.id.Image);
    Bitmap bp = BitmapFactory.decodeFile(imagePath);
    Bitmap resized = Bitmap.createScaledBitmap(bp,(int)(bp.getWidth()*0.7), (int)(bp.getHeight()*0.7), true);
    imageView.setImageBitmap(resized);