从梯形裁剪创建矩形位图
Create rectangle bitmap from trapezoid crop
我想将由点 A、B、C、D 表示的图像的梯形部分转换为矩形位图。
我可以让用户标记图像中的 4 个点,但我不确定如何将梯形转换为矩形。
感谢社区的任何意见。
执行完毕。也感谢@IcedLance 的参考。这是为了将来参考和社区。整个实现已经完成。
private Bitmap trapezoidToRectangleTransform() {
float[] src = new float[8];
Point point = mAnchorPoints.get(0);
src[0] = point.x;
src[1] = point.y;
point = mAnchorPoints.get(1);
src[2] = point.x;
src[3] = point.y;
point = mAnchorPoints.get(3);
src[4] = point.x;
src[5] = point.y;
point = mAnchorPoints.get(2);
src[6] = point.x;
src[7] = point.y;
// set up a dest polygon which is just a rectangle
float[] dst = new float[8];
dst[0] = 0;
dst[1] = 0;
dst[2] = mOriginalBitmap.getWidth();
dst[3] = 0;
dst[4] = mOriginalBitmap.getWidth();
dst[5] = mOriginalBitmap.getHeight();
dst[6] = 0;
dst[7] = mOriginalBitmap.getHeight();
// create a matrix for transformation.
Matrix matrix = new Matrix();
// set the matrix to map the source values to the dest values.
boolean mapped = matrix.setPolyToPoly (src, 0, dst, 0, 4);
float[] mappedTL = new float[] { 0, 0 };
matrix.mapPoints(mappedTL);
int maptlx = Math.round(mappedTL[0]);
int maptly = Math.round(mappedTL[1]);
float[] mappedTR = new float[] { mOriginalBitmap.getWidth(), 0 };
matrix.mapPoints(mappedTR);
int maptry = Math.round(mappedTR[1]);
float[] mappedLL = new float[] { 0, mOriginalBitmap.getHeight() };
matrix.mapPoints(mappedLL);
int mapllx = Math.round(mappedLL[0]);
int shiftX = Math.max(-maptlx, -mapllx);
int shiftY = Math.max(-maptry, -maptly);
Bitmap croppedAndCorrected = null;
if (mapped) {
Bitmap imageOut = Bitmap.createBitmap(mOriginalBitmap, 0, 0, mOriginalBitmap.getWidth(), mOriginalBitmap.getHeight(), matrix, true);
croppedAndCorrected = Bitmap.createBitmap(imageOut, shiftX, shiftY, mOriginalBitmap.getWidth(), mOriginalBitmap.getHeight(), null, true);
imageOut.recycle();
mOriginalBitmap.recycle();
System.gc();
}
return croppedAndCorrected;
}
我想将由点 A、B、C、D 表示的图像的梯形部分转换为矩形位图。
我可以让用户标记图像中的 4 个点,但我不确定如何将梯形转换为矩形。
感谢社区的任何意见。
执行完毕。也感谢@IcedLance 的参考。这是为了将来参考和社区。整个实现已经完成。
private Bitmap trapezoidToRectangleTransform() {
float[] src = new float[8];
Point point = mAnchorPoints.get(0);
src[0] = point.x;
src[1] = point.y;
point = mAnchorPoints.get(1);
src[2] = point.x;
src[3] = point.y;
point = mAnchorPoints.get(3);
src[4] = point.x;
src[5] = point.y;
point = mAnchorPoints.get(2);
src[6] = point.x;
src[7] = point.y;
// set up a dest polygon which is just a rectangle
float[] dst = new float[8];
dst[0] = 0;
dst[1] = 0;
dst[2] = mOriginalBitmap.getWidth();
dst[3] = 0;
dst[4] = mOriginalBitmap.getWidth();
dst[5] = mOriginalBitmap.getHeight();
dst[6] = 0;
dst[7] = mOriginalBitmap.getHeight();
// create a matrix for transformation.
Matrix matrix = new Matrix();
// set the matrix to map the source values to the dest values.
boolean mapped = matrix.setPolyToPoly (src, 0, dst, 0, 4);
float[] mappedTL = new float[] { 0, 0 };
matrix.mapPoints(mappedTL);
int maptlx = Math.round(mappedTL[0]);
int maptly = Math.round(mappedTL[1]);
float[] mappedTR = new float[] { mOriginalBitmap.getWidth(), 0 };
matrix.mapPoints(mappedTR);
int maptry = Math.round(mappedTR[1]);
float[] mappedLL = new float[] { 0, mOriginalBitmap.getHeight() };
matrix.mapPoints(mappedLL);
int mapllx = Math.round(mappedLL[0]);
int shiftX = Math.max(-maptlx, -mapllx);
int shiftY = Math.max(-maptry, -maptly);
Bitmap croppedAndCorrected = null;
if (mapped) {
Bitmap imageOut = Bitmap.createBitmap(mOriginalBitmap, 0, 0, mOriginalBitmap.getWidth(), mOriginalBitmap.getHeight(), matrix, true);
croppedAndCorrected = Bitmap.createBitmap(imageOut, shiftX, shiftY, mOriginalBitmap.getWidth(), mOriginalBitmap.getHeight(), null, true);
imageOut.recycle();
mOriginalBitmap.recycle();
System.gc();
}
return croppedAndCorrected;
}