我正在用相机拍摄图像,然后将其保存到位图中。我期待将该图像居中裁剪为 600x600 像素。
我发现了这样的东西:
https://stackoverflow.com/a/6909144/1943607
但是,我不知道如何设置一个固定的 & 高度。
我在绘制图像和画布方面非常糟糕。这对我来说似乎很抽象。
谢谢你。
我正在用相机拍摄图像,然后将其保存到位图中。我期待将该图像居中裁剪为 600x600 像素。
我发现了这样的东西:
https://stackoverflow.com/a/6909144/1943607
但是,我不知道如何设置一个固定的 & 高度。
我在绘制图像和画布方面非常糟糕。这对我来说似乎很抽象。
谢谢你。
This正是您所需要的。
// Returns an immutable bitmap from the specified subset of the source bitmap.
static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height)
我真的不知道 (0,0) 是左上角还是中心,但我相信它是左上角。
* (0,0)
*~~~~~~+===========+
' ' |
' ' 200 |
' ' |
+~~~~~~+ | 400
| 100 |
| |
| |
+==================+
300
如果它确实是中心,那么:
x should be width /2
y >> height/2
否则,如果它在左上角:
x should be width /2 - cropWidth/2
y >> height/2 - cropHeight/2
在这两种情况下都会看起来像这样。
* (150,200)
+==================+
| |
| +~~~~~~+ |
| ' ' |
| ' * '200 | 400
| ' ' |
| +~~~~~~+ |
| 100 |
+==================+
300
600x600像素
if(srcBmp.getWidth()>600 && srcBmp.getHeight()>600)
dstBmp = Bitmap.createBitmap(srcBmp,
srcBmp.getWidth()/2 - 600/2,
srcBmp.getHeight()/2 - 600/2,
600,
600);
我从您提供的链接中更改了代码,希望这有效。