quantum6 / Android-USB-OTG-Camera

Apache License 2.0
120 stars 37 forks source link

On-connect issue #22

Closed ravi2611gupta closed 1 year ago

ravi2611gupta commented 1 year ago

When I am attaching the OTG camera device I am not able to see the output of that camera and I have to do 2 things to see the output

  1. Restart the app
  2. Select a resolution

and I want to get rid of this problem I want that as I attach the device the output will show on my screen.

quantum6 commented 1 year ago

I can't reduce this question.

ravi2611gupta commented 1 year ago

I have resolved the second problem "Select the resolution" but I am stuck on the first one "Restarting the app". I think it is not a good UX that users have to restart the app after granting permission.

I want to resolve this issue. I just want to show the preview immediately when a user grants permission. I tried it but it is not working. Please let me show you the code.

`private UVCCameraHelper.OnMyDevConnectListener listener = new UVCCameraHelper.OnMyDevConnectListener() {

    @Override
    public void onAttachDev(UsbDevice device) {
        // request open permission
        if (!isRequest) {
            isRequest = true;
            if (mCameraHelper != null) {
                mCameraHelper.requestPermission(0);
                isPreview = true;
                onConnectDev(device, true);
            }
        }
    }

    @Override
    public void onDettachDev(UsbDevice device) {
        // close camera
        if (isRequest) {
            isRequest = false;
            mCameraHelper.closeCamera();
            showShortMsg(device.getDeviceName() + " is out");
        }
    }

    @Override
    public void onConnectDev(UsbDevice device, boolean isConnected) {
        handleDeviceConnection(device, isConnected);
    }

    private void handleDeviceConnection(UsbDevice device, boolean isConnected) {
        if (!isConnected) {
            showShortMsg("fail to connect,please check resolution params");
            isPreview = false;
        } else {
            mCameraHelper.startPreview(mUVCCameraView);
            isPreview = true;
            showShortMsg("connecting");
            // initialize seekbar
            // need to wait UVCCamera initialize over
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Looper.prepare();
                    mCameraHelper.updateResolution(1280, 960);
                    if(mCameraHelper != null && mCameraHelper.isCameraOpened()) {
                        mSeekBrightness.setProgress(mCameraHelper.getModelValue(UVCCameraHelper.MODE_BRIGHTNESS));
                        mSeekContrast.setProgress(mCameraHelper.getModelValue(UVCCameraHelper.MODE_CONTRAST));
                    }
                    Looper.loop();
                }
            }).start();
        }
    }

    @Override
    public void onDisConnectDev(UsbDevice device) {
        showShortMsg("disconnecting");
    }
};

`

ravi2611gupta commented 1 year ago

please help me out it is really important. I just want to show the preview immediately when a user grants permission. I tried it but it is not working.

quantum6 commented 1 year ago

please help me out it is really important. I just want to show the preview immediately when a user grants permission. I tried it but it is not working.

I really don't know how to position the problem, I think you can check the initiate route?

quantum6 commented 1 year ago

Good news! You can modify the USBMonitor.java(178), like this: //Build.VERSION.SDK_INT <= 30 ? 0 : PendingIntent.FLAG_IMMUTABLE); 0);