Использование USB-камеры вместе с камерой смартфона - PullRequest
0 голосов
/ 28 февраля 2019

Я пытаюсь сделать два снимка, а именно, подключив камеру USB к смартфону и используя камеру смартфона.Я использовал код из github (https://github.com/jiangdongguo/AndroidUSBCamera) для захвата изображения с камеры USB. С другой стороны, я пытаюсь попытаться захватить изображение с камеры смартфона с помощью OPENCV.

В соответствии с моим кодом, я могузахватить изображение с помощью USB-камеры и выполнить предварительный просмотр камеры для мобильной камеры, но не могу захватить изображение. Следующее является моей деятельностью.

    public class USBCameraActivity extends AppCompatActivity implements CameraDialog.CameraDialogParent, CameraViewInterface.Callback, CameraBridgeViewBase.CvCameraViewListener2{

    @BindView(R.id.camera_view)
    public View mTextureView;

    @BindView(R.id.seekbar_brightness)
    public SeekBar mSeekBrightness;
    @BindView(R.id.seekbar_contrast)
    public SeekBar mSeekContrast;
    @BindView(R.id.switch_rec_voice)
    public Switch mSwitchVoice;

    myJavaCameraView cameraBridgeViewBase;



    private static final String TAG="USBCameraActivity";
    // myJavaCameraView myJavaCameraView;
    ImageButton mCaptureButton;
    ImageButton mCaptureButton2;
    Mat rgb;


    private BaseLoaderCallback baseLoaderCallback=new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case BaseLoaderCallback.SUCCESS:
                    cameraBridgeViewBase.enableView();
                    break;
                default:
                    super.onManagerConnected(status);
                    break;
            }
        }
    };


    private UVCCameraHelper mCameraHelper;
    private CameraViewInterface mUVCCameraView;
    private AlertDialog mDialog;

    private boolean isRequest;
    private boolean isPreview;

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

        @Override
        public void onAttachDev(UsbDevice device) {
            if (mCameraHelper == null || mCameraHelper.getUsbDeviceCount() == 0) {
                showShortMsg("check no usb camera");
                return;
            }
            // request open permission
            if (!isRequest) {
                isRequest = true;
                if (mCameraHelper != null) {
                    mCameraHelper.requestPermission(0);
                }
            }
        }

        @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) {
            if (!isConnected) {
                showShortMsg("fail to connect,please check resolution params");
                isPreview = false;
            } else {
                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();
                        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");
        }
    };
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_usbcamera);
        //ButterKnife.bind(this);
        //initView();




        //mCaptureButton2.setOnClickListener(mOnClickListener2);

        // step.1 initialize UVCCameraHelper
        mUVCCameraView = (CameraViewInterface) mTextureView;
        mUVCCameraView.setCallback(this);
        mCameraHelper = UVCCameraHelper.getInstance();
        mCameraHelper.setDefaultFrameFormat(UVCCameraHelper.FRAME_FORMAT_YUYV);
        mCameraHelper.initUSBMonitor(this, mUVCCameraView, listener);

        cameraBridgeViewBase=  (myJavaCameraView) findViewById(R.id.java_camera_view);
        cameraBridgeViewBase.setVisibility(JavaCameraView.VISIBLE);
        cameraBridgeViewBase.setCvCameraViewListener(this);

        mCaptureButton= (ImageButton) findViewById(R.id.capture_button);
        mCaptureButton.setVisibility(View.VISIBLE);
        mCaptureButton.setOnClickListener(mOnClickListener);

        mCaptureButton2=(ImageButton) findViewById(R.id.capture_button2);
        mCameraHelper.setOnPreviewFrameListener(new AbstractUVCCameraHandler.OnPreViewResultListener() {
            @Override
            public void onPreviewResult(byte[] nv21Yuv) {

            }
        });





                }




    @Override
    protected void onResume(){
        super.onResume();
        if(OpenCVLoader.initDebug()){
            Log.i(TAG,"success");
            baseLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);

        }
        else{
            Log.d(TAG,"Unsuccessful");
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_11,this,baseLoaderCallback);
        }


    }




    private void initView() {


        mSeekBrightness.setMax(100);
        mSeekBrightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(mCameraHelper != null && mCameraHelper.isCameraOpened()) {
                    mCameraHelper.setModelValue(UVCCameraHelper.MODE_BRIGHTNESS,progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        mSeekContrast.setMax(100);
        mSeekContrast.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(mCameraHelper != null && mCameraHelper.isCameraOpened()) {
                    mCameraHelper.setModelValue(UVCCameraHelper.MODE_CONTRAST,progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        // step.2 register USB event broadcast
        if (mCameraHelper != null) {
            mCameraHelper.registerUSB();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        // step.3 unregister USB event broadcast
        if (mCameraHelper != null) {
            mCameraHelper.unregisterUSB();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_toobar, menu);
        return true;
    }

    protected final View.OnClickListener mOnClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.capture_button:
                    if(mCameraHelper==null || !mCameraHelper.isCameraOpened()){
                        showShortMsg("Camera Open Failed");
                    }
                    String picPath = UVCCameraHelper.ROOT_PATH + "test_"+System.currentTimeMillis()
                            + UVCCameraHelper.SUFFIX_JPEG;



                    mCameraHelper.capturePicture(picPath, new AbstractUVCCameraHandler.OnCaptureListener() {
                        @Override
                        public void onCaptureResult(String path) {
                            Log.i(TAG,"save path:" + path);



                        }
                    });

                    break;


                case R.id.capture_button2:

                    Date sdf=new Date();
                    String currentDateandtime=sdf.toString();
                    String filename=Environment.getExternalStorageDirectory().getPath()+currentDateandtime+".jpeg";

                    cameraBridgeViewBase.takePicture(filename);
                    Toast.makeText(USBCameraActivity.this, "Picture has been taken ", Toast.LENGTH_LONG).show();
                    Log.d(TAG, "Path " + filename);
            }



        }
    };








    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case R.id.menu_recording:
                if (mCameraHelper == null || !mCameraHelper.isCameraOpened()) {
                    showShortMsg("sorry,camera open failed");
                    return super.onOptionsItemSelected(item);
                }
                if (!mCameraHelper.isPushing()) {
                    String videoPath = UVCCameraHelper.ROOT_PATH + System.currentTimeMillis();
                    FileUtils.createfile(FileUtils.ROOT_PATH + "test666.h264");
                    // if you want to record,please create RecordParams like this
                    RecordParams params = new RecordParams();
                    params.setRecordPath(videoPath);
                    params.setRecordDuration(0);
                    params.setVoiceClose(mSwitchVoice.isChecked());    // is close voice
                    mCameraHelper.startPusher(params, new AbstractUVCCameraHandler.OnEncodeResultListener() {
                        @Override
                        public void onEncodeResult(byte[] data, int offset, int length, long timestamp, int type) {
                            // type = 1,h264 video stream
                            if (type == 1) {
                                FileUtils.putFileStream(data, offset, length);
                            }
                            // type = 0,aac audio stream
                            if(type == 0) {

                            }
                        }

                        @Override
                        public void onRecordResult(String videoPath) {
                            Log.i(TAG,"videoPath = "+videoPath);
                        }
                    });
                    // if you only want to push stream,please call like this
                    // mCameraHelper.startPusher(listener);
                    showShortMsg("start record...");
                    mSwitchVoice.setEnabled(false);
                } else {
                    FileUtils.releaseFile();
                    mCameraHelper.stopPusher();
                    showShortMsg("stop record...");
                    mSwitchVoice.setEnabled(true);
                }
                break;
            case R.id.menu_resolution:
                if (mCameraHelper == null || !mCameraHelper.isCameraOpened()) {
                    showShortMsg("sorry,camera open failed");
                    return super.onOptionsItemSelected(item);
                }
                showResolutionListDialog();
                break;
            case R.id.menu_focus:
                if (mCameraHelper == null || !mCameraHelper.isCameraOpened()) {
                    showShortMsg("sorry,camera open failed");
                    return super.onOptionsItemSelected(item);
                }
                mCameraHelper.startCameraFoucs();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showResolutionListDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(USBCameraActivity.this);
        View rootView = LayoutInflater.from(USBCameraActivity.this).inflate(R.layout.layout_dialog_list, null);
        ListView listView = (ListView) rootView.findViewById(R.id.listview_dialog);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(USBCameraActivity.this, android.R.layout.simple_list_item_1, getResolutionList());
        if (adapter != null) {
            listView.setAdapter(adapter);
        }
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                if (mCameraHelper == null || !mCameraHelper.isCameraOpened())
                    return;
                final String resolution = (String) adapterView.getItemAtPosition(position);
                String[] tmp = resolution.split("x");
                if (tmp != null && tmp.length >= 2) {
                    int widht = Integer.valueOf(tmp[0]);
                    int height = Integer.valueOf(tmp[1]);
                    mCameraHelper.updateResolution(widht, height);
                }
                mDialog.dismiss();
            }
        });

        builder.setView(rootView);
        mDialog = builder.create();
        mDialog.show();
    }


    private List<String> getResolutionList() {
        List<Size> list = mCameraHelper.getSupportedPreviewSizes();
        List<String> resolutions = null;
        if (list != null && list.size() != 0) {
            resolutions = new ArrayList<>();
            for (Size size : list) {
                if (size != null) {
                    resolutions.add(size.width + "x" + size.height);
                }
            }
        }
        return resolutions;
    }


    protected void onDestroy() {
        super.onDestroy();
        FileUtils.releaseFile();
        // step.4 release uvc camera resources
        if (mCameraHelper != null) {
            mCameraHelper.release();
        }
        if (cameraBridgeViewBase!=null)
            cameraBridgeViewBase.disableView();
    }

    private void showShortMsg(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public USBMonitor getUSBMonitor() {
        return mCameraHelper.getUSBMonitor();
    }

    @Override
    public void onDialogResult(boolean canceled) {
        if (canceled) {
            showShortMsg("取消操作");
        }
    }

    public boolean isCameraOpened() {
        return mCameraHelper.isCameraOpened();
    }

    @Override
    public void onSurfaceCreated(CameraViewInterface view, Surface surface) {
        if (!isPreview && mCameraHelper.isCameraOpened()) {
            mCameraHelper.startPreview(mUVCCameraView);
            isPreview = true;
        }
    }

    @Override
    public void onSurfaceChanged(CameraViewInterface view, Surface surface, int width, int height) {

    }

    @Override
    public void onSurfaceDestroy(CameraViewInterface view, Surface surface) {
        if (isPreview && mCameraHelper.isCameraOpened()) {
            mCameraHelper.stopPreview();
            isPreview = false;
        }
    }

    @Override
    public void onCameraViewStarted(int width, int height) {
        rgb=new Mat(height,width,CvType.CV_8UC4);


    }

    @Override
    public void onCameraViewStopped() {
        rgb.release();

    }

    @Override
    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        rgb=inputFrame.rgba();
        return rgb;
    }
    }
////////////////////////////////////////////////////    
/////The following is the activity_usbcamera.xml////
////////////////////////////////////////////////////

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff000000"
        tools:context=".view.USBCameraActivity"
        tools:ignore="MergeRootFrame">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:background="@color/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:navigationIcon="@null"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <com.serenegiant.usb.widget.UVCCameraTextureView
            android:id="@+id/camera_view"
            android:layout_width="match_parent"
            android:layout_height="255dp"
            android:layout_alignTop="@id/toolbar"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_marginStart="0dp"
            android:layout_marginLeft="0dp"
            android:layout_marginTop="2dp" />

        <ImageButton
            android:id="@+id/capture_button"
            android:layout_width="54dp"
            android:layout_height="48dp"

            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginEnd="325dp"
            android:layout_marginRight="325dp"
            android:layout_marginBottom="21dp"
            android:background="@null"
            android:scaleType="centerCrop"
            android:src="@android:drawable/ic_menu_camera" />


        <ImageButton
            android:id="@+id/capture_button2"
            android:layout_width="54dp"
            android:layout_height="48dp"

            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="330dp"
            android:layout_marginLeft="330dp"
            android:layout_marginEnd="0dp"
            android:layout_marginRight="0dp"
            android:layout_marginBottom="10dp"
            android:background="@null"
            android:scaleType="centerCrop"
            android:src="@android:drawable/ic_menu_camera"
            android:visibility="visible" />

        <LinearLayout
            android:id="@+id/llayout_contrast"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="10dp"
            android:orientation="horizontal"
            android:visibility="invisible">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:paddingTop="7dp"
                android:paddingBottom="7dp"
                android:text="(contrast)"
                android:textColor="@color/colorWhite"
                android:visibility="invisible" />

            <SeekBar
                android:id="@+id/seekbar_contrast"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/llayout_brightness"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_above="@id/llayout_contrast"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="-29dp"
            android:orientation="horizontal"
            android:visibility="invisible">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:paddingTop="7dp"
                android:paddingBottom="7dp"
                android:text="(brightness)"
                android:textColor="@color/colorWhite"
                android:visibility="invisible" />

            <SeekBar
                android:id="@+id/seekbar_brightness"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:visibility="invisible" />
        </LinearLayout>

        <Switch
            android:id="@+id/switch_rec_voice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/llayout_brightness"
            android:layout_alignStart="@+id/llayout_brightness"
            android:layout_below="@+id/toolbar"
            android:layout_marginLeft="13dp"
            android:layout_marginStart="13dp"
            android:layout_marginTop="27dp"
            android:checked="true"
            android:text="closeVoice"
            android:textColor="@color/colorWhite" />

        <com.jiangdg.usbcamera.view.myJavaCameraView
            android:id="@+id/java_camera_view"
            android:layout_width="396dp"
            android:layout_height="257dp"
            android:layout_alignStart="@+id/llayout_contrast"
            android:layout_alignParentTop="true"
            android:layout_marginTop="294dp" />


    </RelativeLayout>

//////////////////////////////////////////////////////////////////    
////////    The following is the myJavaCameraView.java///////////
////////////////////////////////////////////////////////////////    
        package com.jiangdg.usbcamera.view;

        import android.content.Context;
        import android.hardware.Camera;
        import android.util.AttributeSet;
        import android.util.Log;
        import android.view.SurfaceView;

        import org.opencv.android.JavaCameraView;
        import org.xml.sax.Attributes;

        import java.io.FileOutputStream;

        public class myJavaCameraView extends JavaCameraView implements android.hardware.Camera.PictureCallback {
        static {
            System.loadLibrary("native-lib");
        }

        private static final String TAG="OpenCV";
        private String mPictureFileName;
        private Context context;

        public myJavaCameraView(Context context, AttributeSet attrs)
        {
            super(context, attrs);
            this.context=context;
        }

        public void takePicture(final String fileName){
            Log.i(TAG,"Taking Pictures");
            this.mPictureFileName=fileName;
            mCamera.setPreviewCallback(null);


            mCamera.takePicture(null, null, null);
        }


        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            Log.i(TAG,"Saving");
            mCamera.startPreview();
            mCamera.setPreviewCallback(this);

            try{
                FileOutputStream fos= new FileOutputStream(mPictureFileName);

                fos.write(data);
                fos.close();

            } catch (java.io.IOException e){
                Log.e("PictureDemo","Exception in photoCallback", e);
            }
        }
        }

Может кто-нибудь помочь мне с отладкой этого.

...