java - 如何计算像素位置?
问题描述
我的应用程序的分辨率宽度为 1080 像素,高度为 1920 像素。我使用相机预览,它的分辨率是宽1920,高1080。然后我处理屏幕上的触摸,得到坐标例如x = 250,y = 600。然后使用相机预览帧进行一些处理。但并非一切都是应有的。我认为这是由于分辨率参数的差异。如何更改地方的宽度和高度?然后我在新坐标上画了一个矩形,是根据旧坐标计算出来的,但是相差太大,甚至没有接近结果。
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
if (scanSpace) {
mainActivity = new MainActivity();
Log.d(TAG,"OnPreviewFRAME_START");
Camera.Parameters parameters = camera.getParameters();
int width = parameters.getPreviewSize().width;
int height = parameters.getPreviewSize().height;
Log.d(TAG, "Camera parameters: width: " + String.valueOf(width) + " height: " + String.valueOf(height) );
//^ camera parameters width 1920 , height 1080. - problem. My app display width 1080 height 1920
//convert the byte[] to Bitmap through YuvImage;
//make sure the previewFormat is NV21 (I set it so somewhere before)
YuvImage yuv = new YuvImage(data, parameters.getPreviewFormat(), width, height, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
yuv.compressToJpeg(new Rect(0, 0, width, height), 100, out);
Bitmap bmp = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());
Log.d(TAG, "Frame (picture) parameter: width: " + String.valueOf(bmp.getWidth()) + " height: " + String.valueOf(bmp.getHeight()));
//convert Bitmap to Mat; note the bitmap config ARGB_8888 conversion that
//allows you to use other image processing methods and still save at the end
Log.d(TAG, "BMP size for search: HEIGHT: " + String.valueOf(bmp.getHeight()) + " WIDTH: " + String.valueOf(bmp.getWidth()));
Mat orig = new Mat(bmp.getHeight(), bmp.getWidth(),CvType.CV_8UC3);
bmp = bmp.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(bmp, orig);
Imgproc.cvtColor(orig, orig, Imgproc.COLOR_BGR2GRAY,4);
Log.d(TAG, "Old RES: " + String.valueOf(orig.rows()) + " " + String.valueOf(orig.cols()));
Mat rotMat = new Mat(2, 3, CvType.CV_32FC1);
Mat destination = new Mat(orig.rows(), orig.cols(), orig.type());
Point center = new Point(destination.cols() / 2, destination.rows() / 2);
rotMat = Imgproc.getRotationMatrix2D(center, 90, 1);
Imgproc.warpAffine(orig, destination, rotMat, destination.size());
Log.d(TAG, "NEW RES: " + String.valueOf(destination.rows()) + " " + String.valueOf(destination.cols()));
Mat crop_orig = mainActivity.cropImage(orig);
Log.d(TAG, "CROP IMAGE PARAMS: HEIGHT: " + String.valueOf(crop_orig.height()) + " WIDTH: " + String.valueOf(crop_orig.width()));
Mat result = new Mat();
matchTemplate(orig, crop_orig, result, Imgproc.TM_SQDIFF);
Core.MinMaxLocResult r = Core.minMaxLoc(result);
System.out.println(r.minVal + " " + r.minLoc);
Core.MinMaxLocResult mmr = Core.minMaxLoc(result);
Point matchLoc = mmr.maxLoc;
double tmp_x = matchLoc.x;
double tmp_y = matchLoc.y;
xPoint_1 = (int) tmp_x;
yPoint_1 = (int) tmp_y;
// result not true because diffrent resolution
Log.d(TAG, "FINISH template");
scanSpace = false;
camera.setPreviewCallback(this);
}
}
解决方案
推荐阅读
- swiftui - 有没有更好的方法来实现基于视图选择器选择显示不同的视图?
- python - Keras Dense 输出不是从 0 到 1
- redux-form - ReduxForm 不会在按钮单击时重新呈现
- undefined-behavior - UndefinedBehaviorSanitizer:DEADLYSIGNAL
- jmeter - 如何在jmeter中比较两个结果文件
- sql-server - 打开与多个数据库的连接 - 唤醒
- cmake - 通过工具链或命令行传递 CMAKE_LIBRARY_ARCHITECTURE 重置
- c++ - c ++中的排序函数使整数数组中的所有元素为0
- modelica - 在 Dymola 中使用牛顿求解器进行模拟时出错,但以成功结束
- javascript - 根据另一个对象数组从对象数组中过滤数据