在“图像视图”中缩放图像和设置会降低图像质量并将其压缩

时间:2017-06-02 07:59:33

标签: android android-camera android-imageview android-image android-bitmap

我想要制作一个自定义相机,拍照后我在图像视图中设置它与我设置相机的活动相同。我已成功拍摄照片,但在图像视图中设置图像之前,我必须缩放它,这会降低图像质量。有没有办法显示真实图像而不是缩放它?

我的图像如下所示第一个是相机的真实视图,即表面视图: This is the real view of camera

拍照后会变为:

The real camera image

我使用的代码是:

Camera.PictureCallback picture = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            mCamera.stopPreview();
            surface_view.setVisibility(View.INVISIBLE);
            setupImageDisplay(data);

        }
    };

    private void setupImageDisplay(byte[] data) {
        photo = BitmapFactory.decodeByteArray(data, 0, data.length);
        photo = scaleDown(photo, true);//scaling down bitmap
        imageview_photo.setImageBitmap(photo); //setting bitmap in imageview

    }

    public Bitmap scaleDown(Bitmap realImage, boolean filter) {
        int screenWidth = width;
        int screenHeight = height;

        Bitmap scaled;
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            // Notice that width and height are reversed
            scaled = Bitmap.createScaledBitmap(realImage, screenHeight, screenWidth, filter);
            int w = scaled.getWidth();
            int h = scaled.getHeight();
            // Setting post rotate to 90

            Matrix mtx = new Matrix();

            if (camera_id == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                float[] mirrorY = {-1, 0, 0, 0, 1, 0, 0, 0, 1};
                Matrix matrixMirrorY = new Matrix();
                matrixMirrorY.setValues(mirrorY);

                mtx.postConcat(matrixMirrorY);
            }
            mtx.postRotate(90);
            // Rotating Bitmap
            realImage = Bitmap.createBitmap(scaled, 0, 0, w, h, mtx, filter);
        } else {// LANDSCAPE MODE
            //No need to reverse width and height
            scaled = Bitmap.createScaledBitmap(realImage, screenHeight, screenWidth, filter);
            int w = scaled.getWidth();
            int h = scaled.getHeight();
            // Setting post rotate to 90

            Matrix mtx = new Matrix();

            if (camera_id == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                float[] mirrorY = {-1, 0, 0, 0, 1, 0, 0, 0, 1};
                Matrix matrixMirrorY = new Matrix();
                matrixMirrorY.setValues(mirrorY);

                mtx.postConcat(matrixMirrorY);
            }
            mtx.postRotate(180);
            // Rotating Bitmap
            realImage = Bitmap.createBitmap(scaled, 0, 0, w, h, mtx, filter);
        }
        return realImage;
    }

拍照后,图像就像挤压一样,缩放后图像是否保持不变?

1 个答案:

答案 0 :(得分:0)

您可以创建一个单独的文件,该文件是临时文件并存储图像的缩略图大小。您可以制作这样的POJO来存储两个图像。您可以显示较小的文件并使用原始文件以保持高质量。

public class Image {
File fullSize;
File Thumbnail;

public Image(File fullSize, File thumbnail) {
    this.fullSize = fullSize;
    Thumbnail = thumbnail;
}

public File getFullSize() {
    return fullSize;
}

public void setFullSize(File fullSize) {
    this.fullSize = fullSize;
}

public File getThumbnail() {
    return Thumbnail;
}

public void setThumbnail(File thumbnail) {
    Thumbnail = thumbnail;
}

}