缩放图像并在图像视图中设置会降低图像质量并挤压它
Posted
技术标签:
【中文标题】缩放图像并在图像视图中设置会降低图像质量并挤压它【英文标题】:Scaling the image and setting in Image View reduces image quality and squeezes it 【发布时间】:2017-11-03 13:22:55 【问题描述】:我想做一个自定义相机,拍照后我在图像视图中设置它,与我设置相机的活动相同。我已经成功地拍摄了照片,但在图像视图中设置图像之前,我必须对其进行缩放,这会降低图像质量。有什么方法可以显示真实图像而不是缩放它?
我的图片如下第一个是相机的真实视图,即表面视图:
拍照后变成:
我使用的代码是:
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;
拍照后像被挤压一样,有什么办法可以让缩放后的图像保持不变?
【问题讨论】:
尝试使用 Glide 库,它会自动缩放您的图像。 @MaximeLiege 但在滑行中您不必提供图像的网址吗?在这种情况下,我使用的是简单的位图 不,你也可以给它 Drawables 好的,我会给它位图,让我们看看会发生什么谢谢。 【参考方案1】:您可以创建一个单独的文件,该文件是临时文件并存储图像的缩略图大小。您可以制作这样的 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;
【讨论】:
我应该将图像文件存储在应用程序中吗? 使用权限访问手机的文件存储。并将文件保存在那里,您还应该保存图像的路径。 但我不想将图像存储在手机的任何位置,这样可以吗? 是的..你可以做到。您也可以创建 2 个不同的位图。一种不按比例缩小,一种按比例缩小。并将位图的对象保存在您的应用程序中。 谢谢,我会试试的,会告诉你:)以上是关于缩放图像并在图像视图中设置会降低图像质量并挤压它的主要内容,如果未能解决你的问题,请参考以下文章