简体   繁体   English

在 Android 上使用 RectF 参数裁剪位图

[英]Crop Bitmap using RectF parameters on Android

Using MikeOrtiz's awesome ImageView implementation with touch and zoom events, I wanted to crop a picture taken with the camera to match the zoom.使用 MikeOrtiz 出色的ImageView 实现和触摸和缩放事件,我想裁剪一张用相机拍摄的照片以匹配缩放。 Using his method...用他的方法...

// Return a Rect representing the zoomed image.
RectF getZoomedRect();

...I tried cropping the resulting picture bitmap to the zoom size like so: ...我尝试将生成的图片位图裁剪为缩放大小,如下所示:

RectF zoomCoordinates = mTouchImageView.getZoomedRect();

Bitmap croppedBitmapToOverview = Bitmap.createBitmap(
                AppResources.sCurrentImage,
                ((int) zoomCoordinates.left),
                ((int) zoomCoordinates.top),
                ((int) zoomCoordinates.width()),
                ((int) zoomCoordinates.height()));

However I get a "must be bigger than 0" error with this.但是我得到了一个“必须大于 0”的错误。 While debugging I noticed ALL values were 0 due to casting to an Integer.在调试时,我注意到由于转换为整数,所有值都为 0。 The real values however go something like this:然而,真正的价值是这样的:

//Log.d print for each of those fields without the int cast
Left 0.34047672
Top 0.20797288
Width 0.33333334
Height 0.3429547

So there's my problem, but I can't see how to fix this.所以这是我的问题,但我不知道如何解决这个问题。 I've never worked with bitmaps before or canvas, Rect, etc.我以前从未使用过位图或画布、矩形等。

Is there some tweaking I could do to these values, or should I take a different approach altogether?我可以对这些值做一些调整,还是应该完全采用不同的方法?

Got around the problem by simply taking a "screenshot" of the View of sorts.通过简单地拍摄各种视图的“屏幕截图”来解决这个问题。 This got me a Bitmap with the picture as it was zoomed这让我得到了一张带有图片的位图,因为它被放大了

mTouchImageView.setDrawingCacheEnabled(true);
        AppResources.sCurrentImage = Bitmap.createBitmap(mTouchImageView.getDrawingCache());

将坐标乘以图像的大小,例如

(int)(zoomCoordinates.left * imageSize)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM