lagadic / visp

Open Source Visual Servoing Platform

Home Page:https://visp.inria.fr/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

apriltag detector on android

ViokingTung opened this issue · comments

hi, I set up apriltag detector on android by following this link Tutorial: Creating a simple Android App with ViSP ,it work well, but when I change camera stream data to image, it detect anything, could someone give me some advices, thanks.
my test code:

    @SuppressLint({"WrongThread", "SdCardPath"})
    public void onPreviewFrame(byte[] data, Camera camera) {
        if (System.currentTimeMillis() > 5000 + lastTime) {
            @SuppressLint("SdCardPath") String file_path = "/data/data/test/0000000000000000001.png";
            File file = new File(file_path);
            FileInputStream fis;
            try {
                fis = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while (true) {
                try {
                    if ((len = fis.read(buffer)) == -1) break;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                bos.write(buffer, 0, len);
            }

            VpImageUChar imageUChar = new VpImageUChar(buffer,h,w,true);
            VpDetectorAprilTag detectorAprilTag = new VpDetectorAprilTag();
            List<VpHomogeneousMatrix> matrices = detectorAprilTag.detect(imageUChar,tagSize,cameraParameters);
            Log.d("CameraPreview.java",matrices.size() + " tags detected");
            }

            updateResult(matrices.size() + " 36h11 tags detected within " + (System.currentTimeMillis() - lastTime) +" ms");

            lastTime = System.currentTimeMillis();
        }
    }

referred to this code,rewrote by android, below is an example, hope it can helps someone.

    private static VpImageUChar bitmapToVpImageUChar(Bitmap bitmap){
        Mat mat = new Mat();    //argb
        Bitmap bmp32 = bitmap.copy(Bitmap.Config.ARGB_8888, true);
        Utils.bitmapToMat(bmp32, mat);
        Mat grayMat = new Mat();    //gray
        Imgproc.cvtColor(mat, grayMat, Imgproc.COLOR_RGBA2GRAY);
        Mat resizedMat = new Mat(); //resize
        Imgproc.resize(grayMat, resizedMat, new Size(w, h));
        byte[] imgData = new byte[(int) (resizedMat.total() * resizedMat.channels())];
        resizedMat.get(0, 0, imgData);  //copies the data from the OpenCV matrix into the byte array
        return new VpImageUChar(imgData, h, w, true);
    }