android - 居中裁剪图像以适当的尺寸设置在 ImageView 上

标签 android camera center crop android-framelayout

我正在使用相机 API 来拍照,我必须根据我的 ImageView 大小打开不同尺寸的相机。我正在关注我们在 Android sdk/sample/adroid-18 中获得的名为“ApiDemo”的示例项目,我所做的更改不是在 setcontentview 上设置相机。我已将相机设置为“框架布局”。起初我的相机预览是浆糊的,所以我得到了相机 OptimalPreviewSize 并将 FrameLayout 参数宽度和高度设置为换行内容。现在相机预览比 ImageView 小(我想要的大小)。如果我将 FrameLayout 参数的大小设置为 match-parent,则相机 View 会拉伸(stretch)。如何解决此问题。

找到此链接以获取更多规范。 Android camera preview look strange

更新

我的相机预览尺寸很好,现在我使用布局方法,想法是我有比我的 ImageView 更大的布局,现在相机预览看起来不错。 现在我面临的问题是设置适当尺寸的图像,为此我必须将裁剪和缩放以相同的尺寸居中,就像我的 ImageView 一样。我通过 TakePicture 方法获得此图像并将其保存在 SD 卡中。

为此,我使用此方法:-

    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;
}

但结果图像质量不好。高度角从顶部和底部切割,结果图像质量不好。像素被拉伸(stretch)。

不要告诉我使用scaleType=Center_crop,我在我的情况下不能使用它,并且不想向用户显示裁剪框,这所有过程不应该显示在用户界面上。

更新

我使用打击方法从中心裁剪图像并根据我的 imageView 大小进行缩放

Bitmap dstBmp = ThumbnailUtils.extractThumbnail(source, newWidth, newHeight);

但是我得到的位图看起来与 FrameLayout 上显示的相机预览不一样。因为相机预览很大。我认为这些代码裁剪了很大的区域。 我尝试减小宽度并更改高度,但没有得到我想要的相同裁剪图像。

在图片裁剪后,我还有一个想法,即在 FrameLayout 上自动设置最后一个图像帧。我们可以从框架布局中获取设置的框架吗?这怎么可能?

这是这样的问题 How to retrieve the visible part of a SurfaceView in Android有没有人有解决办法。

我想通过这一行来实现这一点ThumbnailUtils.extractThumbnail(source, newWidth, newHeight); 通过这一行我得到了像图中描述的图像一样的src。

这一行到底要改变什么???

enter image description here

最佳答案

居中裁剪图像可能会对您有所帮助。

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, top, left + scaledWidth, top + scaledHeight);

    // 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;
}

关于android - 居中裁剪图像以适当的尺寸设置在 ImageView 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21107208/

相关文章:

android - 如何创建好看的可绘制对象。

iphone - 是否可以通过 UIActivityViewController 共享图像并保留 exif 数据?

r - ggplot2 中的中心图标题

css - 如何拥有居中的 Bootstrap 布局

android - 从图库中选择图像或从相机拍摄

android - 居中裁剪 Android VideoView

java - android 不压缩保存位图

java - Android 中的 fragment 旋转崩溃问题

android - NestedScrollView 和 WebView 高度问题

opencv - V4L2 绝对曝光设置(几乎)没有效果