以编程方式裁剪位图的底部部分
问题描述:
我从相机取得位图。我想裁剪图像,以便只留下底部。裁剪后的图像应该比原始位图的高度低80%,所以我只需要从左边缘开始的底部20%。以编程方式裁剪位图的底部部分
我在没有任何Android裁剪意图的代码中明确地这样做。
的图像可视化我想达到的目标:
我已经设法通过使用此代码裁剪位图的上半部分:
final Bitmap toBeCropped = BitmapFactory.decodeFile(mFile.getPath());
final BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inTargetDensity = 1;
toBeCropped.setDensity(Bitmap.DENSITY_NONE);
int fromHere = (int) (toBeCropped.getHeight() * 0.2);
Bitmap croppedBitmap = Bitmap.createBitmap(toBeCropped, 0, 0, toBeCropped.getWidth(), fromHere);
mPreviewHalf.setImageBitmap(croppedBitmap);
但我不能没有办法从顶端开始种植80%。我正在考虑获取位图的y坐标,以便我可以裁剪任何图像大小,并且只能获取底部部分。但任何人都可以指向我如何从位图获得这个坐标?或者我必须从布局本身采取它?
答
我不熟悉的位图操作,但是从检查你的代码,并在API看着我的猜测是,你需要指定y坐标上以下行的起点相匹配:
Bitmap croppedBitmap = Bitmap.createBitmap(toBeCropped, 0, "here", toBeCropped.getWidth(), fromHere);
所以我的猜测是类似以下内容:
Bitmap croppedBitmap = Bitmap.createBitmap(toBeCropped, 0, (toBeCropped.getHeight() * 0.8), toBeCropped.getWidth(), fromHere);
在这种情况下
fromHere
将定义要裁剪不是起点(即总数的20%,因为你所指出的那样)的行数
答
,请根据您的需要,我希望用我的代码,它的工作
if (srcBmp.getWidth() >= srcBmp.getHeight()){
dstBmp = Bitmap.createBitmap(
srcBmp,
srcBmp.getWidth()/2 - srcBmp.getHeight()/2,
0,
srcBmp.getHeight(),
srcBmp.getHeight()
);
}else{
dstBmp = Bitmap.createBitmap(
srcBmp,
0,
srcBmp.getHeight()/2 - srcBmp.getWidth()/2,
srcBmp.getWidth(),
srcBmp.getWidth()
);
}
答
这是我要做的事:
topcutoff是你想要的对图像的顶部切出buttomcutoff上buttom(如果需要)
height = height - topcutoff;
height = height - bottomcutoff;
croppedBitmap = Bitmap.createBitmap(croppedBitmap, 0, topcutoff, width, height);
基本上你只是从哪里开始显示位图设置一个起点(topcutoff)。在你的情况下,这将是你的位图80%之后的位置。
这也可以解释一些事情:Google Bitmap Documentation
“INT:在源第一像素的y坐标”,所以要开始显示图像。
谢谢。这是正确的。我最初认为通过的坐标应该是布局的坐标。 – AimanB
没问题,有时你只需要一双新鲜的眼睛。特别是当你花费太多时间看它时。一直发生在我身上! – HarryS