@Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (DEBUG) Log.v(TAG, "onCreate:"); setContentView(R.layout.activity_main); mCameraButton = (ToggleButton) findViewById(R.id.camera_button); mCameraButton.setOnClickListener(mOnClickListener); mCaptureButton = (ImageButton) findViewById(R.id.capture_button); mCaptureButton.setOnClickListener(mOnClickListener); mCaptureButton.setVisibility(View.INVISIBLE); mUVCCameraViewL = (UVCCameraTextureView) findViewById(R.id.camera_view_L); mUVCCameraViewL.setAspectRatio( UVCCamera.DEFAULT_PREVIEW_WIDTH / (float) UVCCamera.DEFAULT_PREVIEW_HEIGHT); mUVCCameraViewL.setSurfaceTextureListener(mSurfaceTextureListener); mUVCCameraViewL.setOnLongClickListener(mOnLongClickListener); mUVCCameraViewR = (UVCCameraTextureView) findViewById(R.id.camera_view_R); mUVCCameraViewR.setAspectRatio( UVCCamera.DEFAULT_PREVIEW_WIDTH / (float) UVCCamera.DEFAULT_PREVIEW_HEIGHT); mUVCCameraViewR.setSurfaceTextureListener(mSurfaceTextureListener); mUVCCameraViewR.setOnLongClickListener(mOnLongClickListener); mUSBMonitor = new USBMonitor(this, mOnDeviceConnectListener); mHandler = UVCCameraHandler.createHandler(this); }
@Override public void onConnect( final UsbDevice device, final UsbControlBlock ctrlBlock, final boolean createNew) { if (DEBUG) Log.v(TAG, "onConnect:"); mHandler.open(ctrlBlock); startPreview(); }
private void startPreview() { mHandler.startPreview(); runOnUiThread( new Runnable() { @Override public void run() { mCaptureButton.setVisibility(View.VISIBLE); } }); }
@Override public void onPause() { if (DEBUG) Log.v(TAG, "onPause:"); // mHandler.stopRecording(); // mHandler.stopPreview(); mHandler.close(); // #close include #stopRecording and #stopPreview mCameraButton.setChecked(false); mCaptureButton.setVisibility(View.INVISIBLE); mUSBMonitor.unregister(); super.onPause(); }
@Override public boolean onLongClick(final View view) { switch (view.getId()) { case R.id.camera_view_L: case R.id.camera_view_R: if (mHandler.isOpened()) { final File outputFile = MediaMuxerWrapper.getCaptureFile(Environment.DIRECTORY_DCIM, ".png"); mHandler.captureStill(outputFile.toString()); try { if (DEBUG) Log.i(TAG, "MediaScannerConnection#scanFile"); MediaScannerConnection.scanFile( getApplicationContext(), new String[] {outputFile.toString()}, null, null); } catch (final Exception e) { Log.e(TAG, "MediaScannerConnection#scanFile:", e); } return true; } } return false; }
@Override public void onDestroy() { if (DEBUG) Log.v(TAG, "onDestroy:"); if (mHandler != null) { mHandler.release(); mHandler = null; } if (mUSBMonitor != null) { mUSBMonitor.destroy(); mUSBMonitor = null; } mUVCCameraViewL = null; mUVCCameraViewR = null; mCameraButton = null; mCaptureButton = null; super.onDestroy(); }
@Override public void onDisconnect(final UsbDevice device, final UsbControlBlock ctrlBlock) { if (DEBUG) Log.v(TAG, "onDisconnect:"); if (mHandler != null) { mHandler.close(); runOnUiThread( new Runnable() { @Override public void run() { if (!isFinishing()) try { mCaptureButton.setVisibility(View.INVISIBLE); mCameraButton.setChecked(false); } catch (final Exception e) { } } }); } }
@Override public void onClick(final View view) { switch (view.getId()) { case R.id.camera_button: if (!mHandler.isOpened()) { CameraDialog.showDialog(MainActivity.this); } else { mHandler.close(); mCaptureButton.setVisibility(View.INVISIBLE); } break; case R.id.capture_button: if (mHandler.isOpened()) { if (!mHandler.isRecording()) { mCaptureButton.setColorFilter(0xffff0000); // turn red mHandler.startRecording(); } else { mCaptureButton.setColorFilter(0); // return to default color mHandler.stopRecording(); } } break; } }
@Override public boolean onSurfaceTextureDestroyed(final SurfaceTexture surface) { mHandler.removeSurface(surface.hashCode()); return true; }
@Override public void onSurfaceTextureAvailable( final SurfaceTexture surface, final int width, final int height) { final Surface _surface = new Surface(surface); mHandler.addSurface(surface.hashCode(), _surface, false, null); }