使用Matrix.setPolyToPoly在位图上选择4个点的区域 [英] Select an area on bitmap with 4 points using Matrix.setPolyToPoly
问题描述
我正在Android上使用位图,并且使用4点在位图上选择区域时遇到了问题.并非所有的4分集都对我有用.在某些情况下,结果只是空白位图,而不是裁剪后的位图(如图片中所示),并且logcat中没有任何错误(甚至是内存错误). 这是我用来进行转换的基本代码.
I am playing with bitmap on Android and I am facing an issue when selecting an area on the bitmap using the 4 points. Not all the sets of 4 points work for me. In some cases, the result is just a blank bitmap instead of the cropped bitmap (like in the picture) and there is not any error in logcat(even memory error). Here is the basic code I used to do the transformation.
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.widget.ImageView;
public class CropImageActivity extends Activity {
private ImageView mCroppedImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.crop);
setupViews();
doCropping();
}
private void doCropping() {
Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample);
//target size
int bitmapWidth = 400;
int bitmapHeight = 400;
Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
//This is one of bad quadangle.
points[0] = 0; //top-left.x
points[1] = 0; //top-left.y
points[2] = 230; //top-right.x
points[3] = 100; //top-right.y
points[4] = 350; //bottom-right.x
points[5] = 350; //bottom-right.y
points[6] = 0; //bottom-left.x
points[7] = 350; //bottom-left.y
float[] src = new float[]{
points[0], points[1],
points[2], points[3],
points[4], points[5],
points[6], points[7]
};
float[] dsc = new float[]{
0, 0,
bitmapWidth, 0,
bitmapWidth, bitmapHeight,
0, bitmapHeight
};
Matrix matrix = new Matrix();
boolean transformResult = matrix.setPolyToPoly(src, 0, dsc, 0, 4);
canvas.drawBitmap(srcBitmap, matrix, null);
mCroppedImageView.setImageBitmap(bitmap);
}
private void setupViews() {
mCroppedImageView = (ImageView) findViewById(R.id.croppedImageView);
}
}
那么,这4个点的坐标会影响画布绘制或矩阵变换吗?感谢您的帮助.
So, does the 4 points coordinates affect the canvas drawing or the matrix transformation? Any help is appreciated.
谢谢
推荐答案
最后,我使用OpenCV解决了我的问题.感谢您在这个问题中的回答!似乎Matrix.setPolyToPoly
并不能在所有情况下都起作用.
Finally, I solve my issue using OpenCV. Thanks for the answer in this question! It seems that Matrix.setPolyToPoly
does not work for all the cases.
这篇关于使用Matrix.setPolyToPoly在位图上选择4个点的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!