如何计算像素位置?

时间:2019-02-08 20:10:26

标签: java android camera preview

我的应用程序的分辨率宽度为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);
    }

    }

0 个答案:

没有答案