我希望有人可以帮助我。我正在制作一个图像处理应用程序,我发现我需要一种更好的方法来加载大图像。
我的计划是迭代图像的“假设”像素(覆盖基本图像宽度/高度的“for循环”,因此每次迭代代表一个像素),缩放/平移/旋转像素位置相对到视图,然后使用此信息来确定视图本身正在显示哪些像素,然后使用BitmapRegionDecoder和BitmapFactory.Options的组合仅加载输出实际需要的图像部分而不是完整(即使缩放图像。
到目前为止,我似乎已经正确地覆盖了图像和平移的比例,但我似乎无法弄清楚如何计算旋转。由于它不是真正的Bitmap像素,我不能使用Matrix.rotate =(这里是视图的onDraw中的图像翻译,imgPosX和imgPosY保持图像的中心点:
m.setTranslate(-userImage.getWidth() / 2.0f, -userImage.getHeight() / 2.0f);
m.postScale(curScale, curScale);
m.postRotate(angle);
m.postTranslate(imgPosX, imgPosY);
mCanvas.drawBitmap(userImage.get(), m, paint);
到目前为止,这是我如何确定图像像素是否在屏幕上的数学方法:
for(int j = 0;j < imageHeight;j++) {
for(int i = 0;i < imageWidth;i++) {
//image starts completely center in view, assume image is original size for simplicity
//this is the original starting position for each pixel
int x = Math.round(((float) viewSizeWidth / 2.0f) - ((float) newImageWidth / 2.0f) + i);
int y = Math.round(((float) viewSizeHeight / 2.0f) - ((float) newImageHeight / 2.0f) + j);
//first we scale the pixel here, easy operation
x = Math.round(x * imageScale);
y = Math.round(y * imageScale);
//now we translate, we do this by determining how many pixels
//our images x/y coordinates have differed from it's original
//starting point, imgPosX and imgPosY in the view start in center
//of view
x = x + Math.round((imgPosX - ((float) viewSizeWidth / 2.0f)));
y = y + Math.round((imgPosY - ((float) viewSizeHeight / 2.0f)));
//TODO need rotation here
}
}
所以,假设我的数学直到旋转正确(可能不是,但它似乎工作到目前为止),那么我如何从像素位置计算旋转?我尝试过其他类似的问题:
不使用旋转我会期望实际出现在屏幕上的像素(我制作的文本文件以1和0的形式输出结果,因此我可以直观地显示屏幕上的内容),但找到了公式在这些问题中,信息不是预期的。 (场景:我旋转了一个图像,所以只有左上角在视图中可见。使用Here中的信息来旋转像素,我应该会看到左上角有一个三角形的1组输出文件,但事实并非如此)
那么,如何在不使用Android矩阵的情况下计算旋转后的像素位置?但仍然得到相同的结果。
如果我完全搞砸了我的道歉=(任何帮助都会受到赞赏,这个项目已经持续了很长时间,我想最终完成大声笑
如果您需要更多信息,我会尽可能多地提供=)感谢您的时间
我意识到这个问题特别困难所以我会在SO允许的情况下立即发布奖金。
答案 0 :(得分:2)
您无需创建自己的Matrix,使用现有的Matrix。 http://developer.android.com/reference/android/graphics/Matrix.html
您可以使用
将位图坐标映射到屏幕坐标float[] coords = {x, y};
m.mapPoints(coords);
float sx = coords[0];
float sy = coords[1];
如果要将屏幕映射到位图坐标,可以创建逆矩阵
Matrix inverse = new Matrix(m);
inverse.inverse();
inverse.mapPoints(...)
我认为你的整体方法会很慢,因为从Java上对CU进行像素处理会产生很多开销。在正常绘制位图时,像素操作在GPU上完成。