Start a new topic
Solved

Pinch to zoom using Input Plugin on Android

Hello,

I am trying to get the pinch to zoom functionality working on Android (Android Javascript API). I am working with the Camera v2. I have pulled some examples online but can't get it to work together with Wikitude. I am using example WikitudeCamera2 from the SimpleInputPlugin. This is where the most of the zooming logic is happening.


  

@Override
    public boolean onTouch(View view, MotionEvent event) {
        try {
            //Activity activity = this;
            //CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(this.getCamera());
            float maxzoom = (characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM))*10;

            Rect m = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
            int action = event.getAction();
            float current_finger_spacing;

            if (event.getPointerCount() > 1) {
                // Multi touch logic
                current_finger_spacing = getFingerSpacing(event);
                if(finger_spacing != 0){
                    if(current_finger_spacing > finger_spacing && maxzoom > zoom_level){
                        zoom_level++;
                    } else if (current_finger_spacing < finger_spacing && zoom_level > 1){
                        zoom_level--;
                    }
                    int minW = (int) (m.width() / maxzoom);
                    int minH = (int) (m.height() / maxzoom);
                    int difW = m.width() - minW;
                    int difH = m.height() - minH;
                    int cropW = difW /100 *(int)zoom_level;
                    int cropH = difH /100 *(int)zoom_level;
                    cropW -= cropW & 3;
                    cropH -= cropH & 3;
                    Rect zoom = new Rect(cropW, cropH, m.width() - cropW, m.height() - cropH);
                    //System.out.print(zoom);
                    Log.v(TAG, zoom.toString());

                    builder.set(CaptureRequest.SCALER_CROP_REGION, zoom);
                }
                finger_spacing = current_finger_spacing;
            } else{
                if (action == MotionEvent.ACTION_UP) {
                    //single touch logic
                }
            }



            try {
                cameraCaptureSession.setRepeatingRequest(builder.build(), null, null);
            } catch (CameraAccessException e) {
                e.printStackTrace();
            } catch (NullPointerException ex) {
                ex.printStackTrace();
            }
        } catch (CameraAccessException e) {
            throw new RuntimeException("can not access camera.", e);
        }
        return true;
    }

 

The zoom rect is calculated correctly but somehow the 


builder.set(CaptureRequest.SCALER_CROP_REGION, zoom);


is not making any effect. I suspect it is because you are using ImageReader to pipe the data to the wikitude input plugin, but all the camera zooming examples are using their own SurfaceViews. 

Could you advise on how would you go implementing this feature?

Thank you


Good morning,



while I can't say for sure why the cropping is not working, your explanation sounds reasonable to me. Especially so since the ImageReader we are using asks for the full resolution of the frame.


ImageReader.newInstance(frameWidth, frameHeight, ImageFormat.YUV_420_888, 2);


Have you tried adjusting these values as well?


If getting a cropped frame from the Camera directly is not working out, you will have to do the cropping yourself. You could either do this on the CPU by working with the character data in the C++ part of the plugin, or on the GPU by rendering the camera frame yourself in the plugin and simply altering the texture coordinates or vertex positions of your full screen quadrilateral.


I suspect the former will be easier to implement. The plugin already has a function that operates on the frame data in order to remove any strides that might be in the frame. You would need to "just" alter the addresses this function copies from.



- Daniel

Thanks Daniel,

the cause of the problem turned out to be trivial - the cropping is not happening on emulator for some reason but works on the device. 


I am marking this as solved but I have run into another issue here: https://support.wikitude.com/support/discussions/topics/5000090364. 


- Tomas

 

Login or Signup to post a comment