diff --git a/app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java b/app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java index b1acd93..0da2000 100644 --- a/app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java +++ b/app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java @@ -9,6 +9,7 @@ import android.support.annotation.Nullable; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.util.Log; +import android.view.Surface; import android.view.View; import android.widget.Button; import android.widget.Toast; @@ -119,10 +120,38 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog setContentView(R.layout.activity_usbcamera); ButterKnife.bind(this); mUVCCameraView = (CameraViewInterface) mTextureView; + mUVCCameraView.setCallback(new CameraViewInterface.Callback() { + @Override + public void onSurfaceCreated(CameraViewInterface view, Surface surface) { + if(!isPreview && mUSBManager.isCameraOpened()) { + mUSBManager.startPreview(mUVCCameraView, new AbstractUVCCameraHandler.OnPreViewResultListener() { + @Override + public void onPreviewResult(boolean result) { + + } + }); + isPreview = true; + } + } + + @Override + public void onSurfaceChanged(CameraViewInterface view, Surface surface, int width, int height) { + + } + + @Override + public void onSurfaceDestroy(CameraViewInterface view, Surface surface) { + if(isPreview && mUSBManager.isCameraOpened()) { + mUSBManager.stopPreview(); + isPreview = false; + } + } + }); // 初始化引擎 mUSBManager = USBCameraManager.getInstance(); mUSBManager.initUSBMonitor(this,listener); mUSBManager.createUVCCamera(mUVCCameraView); + } @Override @@ -132,16 +161,7 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog return; // 注册USB事件广播监听器 mUSBManager.registerUSB(); - if(!isPreview && mUSBManager.isCameraOpened()) { - mUSBManager.startPreview(mUVCCameraView, new AbstractUVCCameraHandler.OnPreViewResultListener() { - @Override - public void onPreviewResult(boolean result) { - - } - }); - isPreview = true; - } - + mUVCCameraView.onResume(); } @Override @@ -151,10 +171,7 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog if(mUSBManager != null){ mUSBManager.unregisterUSB(); } - if(isPreview && mUSBManager.isCameraOpened()) { - mUSBManager.stopPreview(); - isPreview = false; - } + mUVCCameraView.onPause(); } @OnClick({ R.id.btn_contrast,R.id.btn_brightness,R.id.btn_capture_pic, R.id.btn_rec_video,R.id.btn_update_resolution,R.id.btn_restart_camera}) diff --git a/libusbcamera/src/main/java/com/serenegiant/usb/widget/UVCCameraTextureView.java b/libusbcamera/src/main/java/com/serenegiant/usb/widget/UVCCameraTextureView.java index 16ab871..42529e7 100644 --- a/libusbcamera/src/main/java/com/serenegiant/usb/widget/UVCCameraTextureView.java +++ b/libusbcamera/src/main/java/com/serenegiant/usb/widget/UVCCameraTextureView.java @@ -79,10 +79,7 @@ public class UVCCameraTextureView extends AspectRatioTextureView // API >= 14 @Override public void onResume() { if (DEBUG) Log.v(TAG, "onResume:"); -// if (mHasSurface) { -// mRenderHandler = RenderHandler.createHandler(mFpsCounter, super.getSurfaceTexture(), getWidth(), getHeight()); -// } - if (mRenderHandler == null) { + if (mHasSurface) { mRenderHandler = RenderHandler.createHandler(mFpsCounter, super.getSurfaceTexture(), getWidth(), getHeight()); } } @@ -90,10 +87,10 @@ public class UVCCameraTextureView extends AspectRatioTextureView // API >= 14 @Override public void onPause() { if (DEBUG) Log.v(TAG, "onPause:"); -// if (mRenderHandler != null) { -// mRenderHandler.release(); -// mRenderHandler = null; -// } + if (mRenderHandler != null) { + mRenderHandler.release(); + mRenderHandler = null; + } if (mTempBitmap != null) { mTempBitmap.recycle(); mTempBitmap = null; @@ -102,7 +99,7 @@ public class UVCCameraTextureView extends AspectRatioTextureView // API >= 14 @Override public void onSurfaceTextureAvailable(final SurfaceTexture surface, final int width, final int height) { - if (DEBUG) Log.v(TAG, "onSurfaceTextureAvailable:" + surface); + if (DEBUG) Log.i(TAG, "onSurfaceTextureAvailable:" + surface); if (mRenderHandler == null) { mRenderHandler = RenderHandler.createHandler(mFpsCounter, surface, width, height); } else { @@ -116,7 +113,7 @@ public class UVCCameraTextureView extends AspectRatioTextureView // API >= 14 @Override public void onSurfaceTextureSizeChanged(final SurfaceTexture surface, final int width, final int height) { - if (DEBUG) Log.v(TAG, "onSurfaceTextureSizeChanged:" + surface); + if (DEBUG) Log.i(TAG, "onSurfaceTextureSizeChanged:" + surface); if (mRenderHandler != null) { mRenderHandler.resize(width, height); } @@ -127,7 +124,7 @@ public class UVCCameraTextureView extends AspectRatioTextureView // API >= 14 @Override public boolean onSurfaceTextureDestroyed(final SurfaceTexture surface) { - if (DEBUG) Log.v(TAG, "onSurfaceTextureDestroyed:" + surface); + if (DEBUG) Log.i(TAG, "onSurfaceTextureDestroyed:" + surface); if (mRenderHandler != null) { mRenderHandler.release(); mRenderHandler = null;