【发布时间】:2014-02-02 03:19:29
【问题描述】:
我正在使用相机 API 拍照,我必须根据我的图像视图大小打开不同大小的相机。我正在关注我们在 Android sdk/sample/adroid-18 中名为“ApiDemo”的示例项目,我更改的内容不是在 setcontentview 上设置相机。我已将相机设置为帧布局。起初我的相机预览是固定的,所以我得到了相机 OptimalPreviewSize 并将 FrameLayout 参数宽度和高度设置为 wrap-content。现在相机预览比 ImageView 小(我想要的尺寸)。如果我将 FrameLayout 参数的大小设置为匹配父级,则相机视图是拉伸的。如何解决此问题。
查找此链接以获取更多规范。 Android camera preview look strange
更新
我的相机预览尺寸很好,现在我使用 on Layout 方法,我的想法是我的布局比我的 ImageView 更大,现在相机预览看起来不错。 现在我面临的问题是为此设置适当大小的图像我必须像我的 ImageView 一样以相同的大小居中裁剪和缩放。我通过 TakePicture 方法获得并保存在 sdcard 中的这个图像。
为此我正在使用这种方法:-
public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
// Compute the scaling factors to fit the new height and width, respectively.
// To cover the final image, the final scaling will be the bigger
// of these two.
float xScale = (float) newWidth / sourceWidth;
float yScale = (float) newHeight / sourceHeight;
float scale = Math.max(xScale, yScale);
// Now get the size of the source bitmap when scaled
float scaledWidth = scale * sourceWidth;
float scaledHeight = scale * sourceHeight;
// Let's find out the upper left coordinates if the scaled bitmap
// should be centered in the new size give by the parameters
float left = (newWidth - scaledWidth) / 2;
float top = (newHeight - scaledHeight) / 2;
// The target rectangle for the new, scaled version of the source bitmap will now
// be
RectF targetRect = new RectF(left+50, top, left + scaledWidth, top + scaledHeight+50);
// RectF targetRect = new RectF(0, 0, newWidth, newHeight/2);
// Finally, we create a new bitmap of the specified size and draw our new,
// scaled bitmap onto it.
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
Canvas canvas = new Canvas(dest);
canvas.drawBitmap(source, null, targetRect, null);
return dest;
}
但是结果图像质量不好。高度角从顶部和底部切割,结果图像质量不好。像素被拉伸。
不要告诉我使用 scaleType=Center_crop 我不能在我的情况下使用它,也不想向用户显示裁剪框,这所有过程都不应该显示在 UI 上。
更新
我使用打击方法从中心裁剪图像并根据我的 imageView 大小进行缩放
Bitmap dstBmp = ThumbnailUtils.extractThumbnail(source, newWidth, newHeight);
但我得到的位图与 FrameLayout 上显示的相机预览不同。因为相机预览很大。我认为这些代码裁剪了大面积。 我试图减小宽度并改变高度,但没有得到我想要的相同比例的裁剪图像。
在图片裁剪后,我还有一个想法是在 FrameLayout 上自动设置最后一个图像帧。我们可以从 Frame Layout 中获取该设置的框架吗?这怎么可能?
这是How to retrieve the visible part of a SurfaceView in Android这样的问题,有人有解决方案吗?
我想通过这一行 ThumbnailUtils.extractThumbnail(source, newWidth, newHeight); 来实现这一点,并且通过这一行,我得到了类似于图中描述的图像的 src。
这行到底要改什么????
【问题讨论】:
-
张贴你看到的图片的快照
-
完成!请检查更新后的帖子
-
非常简单的解决方案:stackoverflow.com/a/17733530/294884
标签: android camera center crop android-framelayout