Android (裁剪图像)将裁剪矩形从缩放图像映射到原始图像
我正试图在Android中实现图像裁剪,源代码来自AOSP gallery,应用程序代码实际上取自此。我已将ImageView的缩放类型设置为矩阵,并在图像矩阵上使用后平移和后缩放方法缩放和平移图像 在图像上绘制固定260x260像素的裁剪矩形,在ImageView中居中。此裁剪矩形的大小固定,无法移动。相反,允许移动和缩放图像放大/缩小,以便用户可以将图像的所需区域放置在裁剪矩形内 当用户通过dx、dy数量、posttranslatedx移动图像时,会在图像的矩阵上调用dy。然后通过调用matrix.maprect将裁剪矩形映射到当前矩阵。然后,通过将dx、dy转换为图像空间并相应移动裁剪矩形,计算裁剪矩形在原始图像上的新位置 下面是一些移动图像的源代码摘录:Android (裁剪图像)将裁剪矩形从缩放图像映射到原始图像,android,image-processing,imageview,Android,Image Processing,Imageview,我正试图在Android中实现图像裁剪,源代码来自AOSP gallery,应用程序代码实际上取自此。我已将ImageView的缩放类型设置为矩阵,并在图像矩阵上使用后平移和后缩放方法缩放和平移图像 在图像上绘制固定260x260像素的裁剪矩形,在ImageView中居中。此裁剪矩形的大小固定,无法移动。相反,允许移动和缩放图像放大/缩小,以便用户可以将图像的所需区域放置在裁剪矩形内 当用户通过dx、dy数量、posttranslatedx移动图像时,会在图像的矩阵上调用dy。然后通过调用mat
// This matrix is recomputed when we go from the thumbnail image to
// the full size image.
protected Matrix baseMatrix = new Matrix();
// This is the supplementary transformation which reflects what
// the user has done in terms of zooming and panning.
//
// This matrix remains the same when we go from the thumbnail image
// to the full size image.
protected Matrix suppMatrix = new Matrix();
// This is the final matrix which is computed as the concatentation
// of the base matrix and the supplementary matrix.
private final Matrix displayMatrix = new Matrix();
//move image by dx dy
protected void panBy(float dx, float dy) {
postTranslate(dx, dy);
setImageMatrix(getImageViewMatrix());
}
protected void postTranslate(float dx, float dy) {
suppMatrix.postTranslate(dx, dy);
}
// Combine the base matrix and the supp matrix to make the final matrix
protected Matrix getImageViewMatrix() {
// The final matrix is computed as the concatentation of the base matrix
// and the supplementary matrix
displayMatrix.set(baseMatrix);
displayMatrix.postConcat(suppMatrix);
return displayMatrix;
}
这就是图像移动如何影响裁剪矩形视图
public void handlePan(float dx, float dy)
{
RectF r = computeLayout();
float xDelta = (dx * (cropRect.width() / r.width()));
float yDelta = -(dy * (cropRect.height() / r.height()));
cropRect.offset(xDelta, yDelta);
// Put the cropping rectangle inside image rectangle
cropRect.offset(
Math.max(0, imageRect.left - cropRect.left),
Math.max(0, imageRect.top - cropRect.top));
cropRect.offset(
Math.min(0, imageRect.right - cropRect.right),
Math.min(0, imageRect.bottom - cropRect.bottom));
}
private RectF computeLayout() {
RectF r = new RectF(cropRect.left, cropRect.top,
cropRect.right, cropRect.bottom);
matrix.mapRect(r);
return r;
}
裁剪视图通过以下getunrotatedmatrix函数获取图像的当前矩阵
这很好用。但我正在努力处理图像的缩放。我尝试将一个矩形映射到矩阵,然后将该矩形映射到原始图像,将裁剪矩形缩小到与图像放大相同的大小,反之亦然。但没有一个成功。在imageview中缩放图像后,如何在SD卡上获取裁剪矩形在原始图像上的位置?缩放操作如下所示:
protected void zoomTo(float scaleFactor, float centerX, float centerY) {
float oldScale = getScale();
float scale = oldScale*scaleFactor;
if (scale > maxZoom) {
scale = maxZoom;
scaleFactor=maxZoom/oldScale;
}
else if(scale<0.5)
{
scale=0.5f;
scaleFactor=0.5f/oldScale;
}
suppMatrix.postScale(scaleFactor, scaleFactor, centerX, centerY);
setImageMatrix(getImageViewMatrix());
}
我知道在放大后,裁剪矩形必须变小,以指向原始图像的较小区域,反之亦然。但是如何精确地进行缩放并适当地定位裁剪矩形,考虑到缩放中心可以位于图像上的任何位置,甚至在裁剪矩形之外 使用反转矩阵将矩形映射回原来的位置,而不是放大后将其缩小到应该的位置。假设RectF clip是一个剪辑矩形,然后调用invertedMatrix.mapRectoutRect,clipok,我会尝试让您知道。不起作用。只需将矩形移出图像的边界即可。
protected void zoomTo(float scaleFactor, float centerX, float centerY) {
float oldScale = getScale();
float scale = oldScale*scaleFactor;
if (scale > maxZoom) {
scale = maxZoom;
scaleFactor=maxZoom/oldScale;
}
else if(scale<0.5)
{
scale=0.5f;
scaleFactor=0.5f/oldScale;
}
suppMatrix.postScale(scaleFactor, scaleFactor, centerX, centerY);
setImageMatrix(getImageViewMatrix());
}