Browse Source

修复华为、小米、vivo等机型,重复预览失败bug

main 1.3.8
jiangdongguo 7 years ago
parent
commit
db0e66d74d
  1. 45
      app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java
  2. 19
      libusbcamera/src/main/java/com/serenegiant/usb/widget/UVCCameraTextureView.java

45
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.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.util.Log; import android.util.Log;
import android.view.Surface;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.Toast; import android.widget.Toast;
@ -119,10 +120,38 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog
setContentView(R.layout.activity_usbcamera); setContentView(R.layout.activity_usbcamera);
ButterKnife.bind(this); ButterKnife.bind(this);
mUVCCameraView = (CameraViewInterface) mTextureView; 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 = USBCameraManager.getInstance();
mUSBManager.initUSBMonitor(this,listener); mUSBManager.initUSBMonitor(this,listener);
mUSBManager.createUVCCamera(mUVCCameraView); mUSBManager.createUVCCamera(mUVCCameraView);
} }
@Override @Override
@ -132,16 +161,7 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog
return; return;
// 注册USB事件广播监听器 // 注册USB事件广播监听器
mUSBManager.registerUSB(); mUSBManager.registerUSB();
if(!isPreview && mUSBManager.isCameraOpened()) { mUVCCameraView.onResume();
mUSBManager.startPreview(mUVCCameraView, new AbstractUVCCameraHandler.OnPreViewResultListener() {
@Override
public void onPreviewResult(boolean result) {
}
});
isPreview = true;
}
} }
@Override @Override
@ -151,10 +171,7 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog
if(mUSBManager != null){ if(mUSBManager != null){
mUSBManager.unregisterUSB(); mUSBManager.unregisterUSB();
} }
if(isPreview && mUSBManager.isCameraOpened()) { mUVCCameraView.onPause();
mUSBManager.stopPreview();
isPreview = false;
}
} }
@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}) @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})

19
libusbcamera/src/main/java/com/serenegiant/usb/widget/UVCCameraTextureView.java

@ -79,10 +79,7 @@ public class UVCCameraTextureView extends AspectRatioTextureView // API >= 14
@Override @Override
public void onResume() { public void onResume() {
if (DEBUG) Log.v(TAG, "onResume:"); if (DEBUG) Log.v(TAG, "onResume:");
// if (mHasSurface) { if (mHasSurface) {
// mRenderHandler = RenderHandler.createHandler(mFpsCounter, super.getSurfaceTexture(), getWidth(), getHeight());
// }
if (mRenderHandler == null) {
mRenderHandler = RenderHandler.createHandler(mFpsCounter, super.getSurfaceTexture(), getWidth(), getHeight()); mRenderHandler = RenderHandler.createHandler(mFpsCounter, super.getSurfaceTexture(), getWidth(), getHeight());
} }
} }
@ -90,10 +87,10 @@ public class UVCCameraTextureView extends AspectRatioTextureView // API >= 14
@Override @Override
public void onPause() { public void onPause() {
if (DEBUG) Log.v(TAG, "onPause:"); if (DEBUG) Log.v(TAG, "onPause:");
// if (mRenderHandler != null) { if (mRenderHandler != null) {
// mRenderHandler.release(); mRenderHandler.release();
// mRenderHandler = null; mRenderHandler = null;
// } }
if (mTempBitmap != null) { if (mTempBitmap != null) {
mTempBitmap.recycle(); mTempBitmap.recycle();
mTempBitmap = null; mTempBitmap = null;
@ -102,7 +99,7 @@ public class UVCCameraTextureView extends AspectRatioTextureView // API >= 14
@Override @Override
public void onSurfaceTextureAvailable(final SurfaceTexture surface, final int width, final int height) { 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) { if (mRenderHandler == null) {
mRenderHandler = RenderHandler.createHandler(mFpsCounter, surface, width, height); mRenderHandler = RenderHandler.createHandler(mFpsCounter, surface, width, height);
} else { } else {
@ -116,7 +113,7 @@ public class UVCCameraTextureView extends AspectRatioTextureView // API >= 14
@Override @Override
public void onSurfaceTextureSizeChanged(final SurfaceTexture surface, final int width, final int height) { 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) { if (mRenderHandler != null) {
mRenderHandler.resize(width, height); mRenderHandler.resize(width, height);
} }
@ -127,7 +124,7 @@ public class UVCCameraTextureView extends AspectRatioTextureView // API >= 14
@Override @Override
public boolean onSurfaceTextureDestroyed(final SurfaceTexture surface) { public boolean onSurfaceTextureDestroyed(final SurfaceTexture surface) {
if (DEBUG) Log.v(TAG, "onSurfaceTextureDestroyed:" + surface); if (DEBUG) Log.i(TAG, "onSurfaceTextureDestroyed:" + surface);
if (mRenderHandler != null) { if (mRenderHandler != null) {
mRenderHandler.release(); mRenderHandler.release();
mRenderHandler = null; mRenderHandler = null;

Loading…
Cancel
Save