- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我创建了一个基于连接网络摄像头的 android 应用程序。该应用程序在连接网络摄像头时发挥作用。但是当我拔下插头时,我的手机应用程序崩溃并显示“不幸的是应用程序已停止工作”。
视频 Activity :
public class VideoActivity extends AppCompatActivity {
ActionBar actionBar;
public static int mCurrentPosition = -1;
private Handler handler;
private Runnable mRunnable;
//--------------------------------
private static final boolean DEBUG = true;
private static final String TAG = "VIDEO ACTIVITY";
private static final int DEFAULT_WIDTH = 640; //640
private static final int DEFAULT_HEIGHT = 480; //480
private USBMonitor mUSBMonitor;
private ICameraClient mCameraClient;
private CameraViewInterface mCameraView;
private boolean isSubView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_video_layout);
if (mUSBMonitor == null) {
mUSBMonitor = new USBMonitor(getApplicationContext(), mOnDeviceConnectListener);
final List<DeviceFilter> filters = DeviceFilter.getDeviceFilters(getApplicationContext(), R.xml.device_filter);
mUSBMonitor.setDeviceFilter(filters);
}
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getInt("STATE");
}
actionBar = getSupportActionBar();
assert actionBar != null;
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.custom_actionbar, null);
actionBar.setCustomView(v);
mCameraView = (CameraViewInterface) findViewById(R.id.camera_view);
mCameraView.setAspectRatio(DEFAULT_WIDTH / (float) DEFAULT_HEIGHT);
mCameraView.setCallback(mCallback);
}
@Override
public void onBackPressed() {
finish();
moveTaskToBack(true);
super.onBackPressed();
}
private final USBMonitor.OnDeviceConnectListener mOnDeviceConnectListener = new USBMonitor.OnDeviceConnectListener() {
@Override
public void onAttach(final UsbDevice device) {
if (DEBUG) Log.v(TAG, "OnDeviceConnectListener#onAttach:");
if (!updateCameraDialog() && (mCameraView.getSurface() != null)) {
tryOpenUVCCamera(true);
}
}
@Override
public void onConnect(final UsbDevice device, final USBMonitor.UsbControlBlock ctrlBlock, final boolean createNew) {
if (DEBUG) Log.v(TAG, "OnDeviceConnectListener#onConnect:");
}
@Override
public void onDisconnect(final UsbDevice device, final USBMonitor.UsbControlBlock ctrlBlock) {
if (DEBUG) Log.v(TAG, "OnDeviceConnectListener#onDisconnect:");
}
@Override
public void onDettach(final UsbDevice device) {
if (DEBUG) Log.v(TAG, "OnDeviceConnectListener#onDettach:");
if (mCameraClient != null) {
mCameraClient.disconnect();
mCameraClient.release();
mCameraClient = null;
}
updateCameraDialog();
}
@Override
public void onCancel() {
if (DEBUG) Log.v(TAG, "OnDeviceConnectListener#onCancel:");
}
};
private boolean updateCameraDialog() {
final Fragment fragment = getFragmentManager().findFragmentByTag("CameraDialog");
if (fragment instanceof CameraDialog) {
((CameraDialog) fragment).updateDevices();
return true;
}
return false;
}
private void tryOpenUVCCamera(final boolean requestPermission) {
if (DEBUG) Log.v(TAG, "tryOpenUVCCamera:");
openUVCCamera(0);
}
public void openUVCCamera(final int index) {
if (DEBUG) Log.v(TAG, "openUVCCamera:index=" + index);
if (!mUSBMonitor.isRegistered()) return;
final List<UsbDevice> list = mUSBMonitor.getDeviceList();
if (list.size() > index) {
if (mCameraClient == null)
mCameraClient = new CameraClient(getApplicationContext(), mCameraListener);
mCameraClient.select(list.get(index));
mCameraClient.resize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
mCameraClient.connect();
}
}
private final CameraViewInterface.Callback mCallback = new CameraViewInterface.Callback() {
@Override
public void onSurfaceCreated(final Surface surface) {
tryOpenUVCCamera(true);
}
@Override
public void onSurfaceChanged(final Surface surface, final int width, final int height) {
}
@Override
public void onSurfaceDestroy(final Surface surface) {
}
};
private final ICameraClientCallback mCameraListener = new ICameraClientCallback() {
@Override
public void onConnect() {
if (DEBUG) Log.v(TAG, "onConnect:");
mCameraClient.addSurface(mCameraView.getSurface(), false);
isSubView = true;
}
@Override
public void onDisconnect() {
if (DEBUG) Log.v(TAG, "onDisconnect:");
}
};}
UVC相机:
public class UVCService extends Service {
private static final boolean DEBUG = true;
private static final boolean checkConn = true;
private static final String TAG = "UVCService";
private USBMonitor mUSBMonitor;
public UVCService() {
if (DEBUG) Log.d(TAG, "Constructor:");
}
@Override
public void onCreate() {
super.onCreate();
if (DEBUG) Log.d(TAG, "onCreate:");
if (mUSBMonitor == null) {
mUSBMonitor = new USBMonitor(getApplicationContext(), mOnDeviceConnectListener);
mUSBMonitor.register();
}
}
@Override
public void onDestroy() {
if (DEBUG) Log.d(TAG, "onDestroy:");
if (checkReleaseService()) {
if (mUSBMonitor != null) {
mUSBMonitor.unregister();
mUSBMonitor = null;
}
}
super.onDestroy();
}
@Override
public IBinder onBind(final Intent intent) {
if (DEBUG) Log.d(TAG, "onBind:" + intent);
if (IUVCService.class.getName().equals(intent.getAction())) {
Log.i(TAG, "return mBasicBinder");
return mBasicBinder;
}
if (IUVCSlaveService.class.getName().equals(intent.getAction())) {
Log.i(TAG, "return mSlaveBinder");
return mSlaveBinder;
}
return null;
}
@Override
public void onRebind(final Intent intent) {
if (DEBUG)
Log.d(TAG, "onRebind:" + intent);
}
@Override
public boolean onUnbind(final Intent intent) {
if (DEBUG)
Log.d(TAG, "onUnbind:" + intent);
if (checkReleaseService()) {
mUSBMonitor.unregister();
mUSBMonitor = null;
}
return true;
}
//********************************************************************************
private final OnDeviceConnectListener mOnDeviceConnectListener = new OnDeviceConnectListener() {
@Override
public void onAttach(final UsbDevice device) {
if (DEBUG) Log.d(TAG, "OnDeviceConnectListener#onAttach:");
}
@Override
public void onConnect(final UsbDevice device, final UsbControlBlock ctrlBlock, final boolean createNew) {
if (DEBUG) Log.d(TAG, "OnDeviceConnectListener#onConnect:");
final int key = device.hashCode();
CameraServer service;
synchronized (sServiceSync) {
service = sCameraServers.get(key);
if (service == null) {
service = CameraServer.createServer(UVCService.this, ctrlBlock, device.getVendorId(), device.getProductId());
sCameraServers.append(key, service);
} else {
Log.w(TAG, "service already exist before connection");
}
sServiceSync.notifyAll();
}
}
@Override
public void onDisconnect(final UsbDevice device, final UsbControlBlock ctrlBlock) {
if (DEBUG) Log.d(TAG, "OnDeviceConnectListener#onDisconnect:");
if (!checkConn) {
processStopService(UVCService.TAG);
}
removeService(device);
/*if(!checkConn){this.stopService(new Intent(this, MyService.class));}
;*/
}
@Override
public void onDettach(final UsbDevice device) {
if (DEBUG) Log.d(TAG, "OnDeviceConnectListener#onDettach:");
if (!checkConn) {
processStopService(UVCService.TAG);
}
removeService(device);
}
@Override
public void onCancel() {
if (DEBUG) Log.d(TAG, "OnDeviceConnectListener#onCancel:");
synchronized (sServiceSync) {
sServiceSync.notifyAll();
}
}
};
// ======================================================================================
private void processStopService(final String tag) {
Intent intent = new Intent(getApplicationContext(), UVCService.class);
intent.addCategory(tag);
stopService(intent);
}
// ======================================================================================
private void removeService(final UsbDevice device) {
final int key = device.hashCode();
synchronized (sServiceSync) {
final CameraServer service = sCameraServers.get(key);
if (service != null)
service.release();
sCameraServers.remove(key);
sServiceSync.notifyAll();
}
if (checkReleaseService()) {
if (mUSBMonitor != null) {
mUSBMonitor.unregister();
mUSBMonitor = null;
}
}
}
//********************************************************************************
private static final Object sServiceSync = new Object();
private static final SparseArray<CameraServer> sCameraServers = new SparseArray<CameraServer>();
/**
* get CameraService that has specific ID<br>
* if zero is provided as ID, just return top of CameraServer instance(non-blocking method) if exists or null.<br>
* if non-zero ID is provided, return specific CameraService if exist. block if not exists.<br>
* return null if not exist matched specific ID<br>
*
* @param serviceId
* @return
*/
private static CameraServer getCameraServer(final int serviceId) {
synchronized (sServiceSync) {
CameraServer server = null;
if ((serviceId == 0) && (sCameraServers.size() > 0)) {
server = sCameraServers.valueAt(0);
} else {
server = sCameraServers.get(serviceId);
if (server == null)
try {
Log.i(TAG, "waitting for service is ready");
sServiceSync.wait();
} catch (final InterruptedException e) {
}
server = sCameraServers.get(serviceId);
}
return server;
}
}
/**
* @return true if there are no camera connection
*/
private static boolean checkReleaseService() {
CameraServer server = null;
synchronized (sServiceSync) {
final int n = sCameraServers.size();
if (DEBUG) Log.d(TAG, "checkReleaseService:number of service=" + n);
for (int i = 0; i < n; i++) {
server = sCameraServers.valueAt(i);
Log.i(TAG, "checkReleaseService:server=" + server + ",isConnected=" + (server != null ? server.isConnected() : false));
if (server != null && !server.isConnected()) {
sCameraServers.removeAt(i);
server.release();
}
}
return sCameraServers.size() == 0;
}
}
//********************************************************************************
private final IUVCService.Stub mBasicBinder = new IUVCService.Stub() {
private IUVCServiceCallback mCallback;
@Override
public int select(final UsbDevice device, final IUVCServiceCallback callback) throws RemoteException {
if (DEBUG)
Log.d(TAG, "mBasicBinder#select:device=" + (device != null ? device.getDeviceName() : null));
mCallback = callback;
final int serviceId = device.hashCode();
CameraServer server = null;
synchronized (sServiceSync) {
server = sCameraServers.get(serviceId);
if (server == null) {
Log.i(TAG, "request permission");
mUSBMonitor.requestPermission(device);
Log.i(TAG, "wait for getting permission");
try {
sServiceSync.wait();
} catch (final Exception e) {
Log.e(TAG, "connect:", e);
}
Log.i(TAG, "check service again");
server = sCameraServers.get(serviceId);
if (server == null) {
throw new RuntimeException("failed to open USB device(has no permission)");
}
}
}
if (server != null) {
Log.i(TAG, "success to get service:serviceId=" + serviceId);
server.registerCallback(callback);
}
return serviceId;
}
@Override
public void release(final int serviceId) throws RemoteException {
if (DEBUG) Log.d(TAG, "mBasicBinder#release:");
synchronized (sServiceSync) {
final CameraServer server = sCameraServers.get(serviceId);
if (server != null) {
if (server.unregisterCallback(mCallback)) {
if (!server.isConnected()) {
sCameraServers.remove(serviceId);
if (server != null) {
server.release();
}
final CameraServer srv = sCameraServers.get(serviceId);
Log.w(TAG, "srv=" + srv);
}
}
}
}
mCallback = null;
}
@Override
public boolean isSelected(final int serviceId) throws RemoteException {
return getCameraServer(serviceId) != null;
}
@Override
public void releaseAll() throws RemoteException {
if (DEBUG) Log.d(TAG, "mBasicBinder#releaseAll:");
CameraServer server;
synchronized (sServiceSync) {
final int n = sCameraServers.size();
for (int i = 0; i < n; i++) {
server = sCameraServers.valueAt(i);
sCameraServers.removeAt(i);
if (server != null) {
server.release();
}
}
}
}
@Override
public void resize(final int serviceId, final int width, final int height) {
if (DEBUG) Log.d(TAG, "mBasicBinder#resize:");
final CameraServer server = getCameraServer(serviceId);
if (server == null) {
throw new IllegalArgumentException("invalid serviceId");
}
server.resize(width, height);
}
@Override
public void connect(final int serviceId) throws RemoteException {
if (DEBUG) Log.d(TAG, "mBasicBinder#connect:");
final CameraServer server = getCameraServer(serviceId);
if (server == null) {
throw new IllegalArgumentException("invalid serviceId");
}
server.connect();
}
@Override
public void disconnect(final int serviceId) throws RemoteException {
if (DEBUG) Log.d(TAG, "mBasicBinder#disconnect:");
final CameraServer server = getCameraServer(serviceId);
if (server == null) {
throw new IllegalArgumentException("invalid serviceId");
}
server.disconnect();
}
@Override
public boolean isConnected(final int serviceId) throws RemoteException {
final CameraServer server = getCameraServer(serviceId);
return (server != null) && server.isConnected();
}
@Override
public void addSurface(final int serviceId, final int id_surface, final Surface surface, final boolean isRecordable) throws RemoteException {
if (DEBUG)
Log.d(TAG, "mBasicBinder#addSurface:id=" + id_surface + ",surface=" + surface);
final CameraServer server = getCameraServer(serviceId);
if (server != null)
server.addSurface(id_surface, surface, isRecordable, null);
}
@Override
public void removeSurface(final int serviceId, final int id_surface) throws RemoteException {
if (DEBUG) Log.d(TAG, "mBasicBinder#removeSurface:id=" + id_surface);
final CameraServer server = getCameraServer(serviceId);
if (server != null)
server.removeSurface(id_surface);
}
@Override
public boolean isRecording(final int serviceId) throws RemoteException {
final CameraServer server = getCameraServer(serviceId);
return server != null && server.isRecording();
}
@Override
public void startRecording(final int serviceId) throws RemoteException {
if (DEBUG) Log.d(TAG, "mBasicBinder#startRecording:");
final CameraServer server = getCameraServer(serviceId);
if ((server != null) && !server.isRecording()) {
server.startRecording();
}
}
@Override
public void stopRecording(final int serviceId) throws RemoteException {
if (DEBUG) Log.d(TAG, "mBasicBinder#stopRecording:");
final CameraServer server = getCameraServer(serviceId);
if ((server != null) && server.isRecording()) {
server.stopRecording();
}
}
@Override
public void captureStillImage(final int serviceId, final String path) throws RemoteException {
if (DEBUG) Log.d(TAG, "mBasicBinder#captureStillImage:" + path);
final CameraServer server = getCameraServer(serviceId);
if (server != null) {
server.captureStill(path);
}
}
};
//********************************************************************************
private final IUVCSlaveService.Stub mSlaveBinder = new IUVCSlaveService.Stub() {
@Override
public boolean isSelected(final int serviceID) throws RemoteException {
return getCameraServer(serviceID) != null;
}
@Override
public boolean isConnected(final int serviceID) throws RemoteException {
final CameraServer server = getCameraServer(serviceID);
return server != null ? server.isConnected() : false;
}
@Override
public void addSurface(final int serviceID, final int id_surface, final Surface surface, final boolean isRecordable, final IUVCServiceOnFrameAvailable callback) throws RemoteException {
if (DEBUG)
Log.d(TAG, "mSlaveBinder#addSurface:id=" + id_surface + ",surface=" + surface);
final CameraServer server = getCameraServer(serviceID);
if (server != null) {
server.addSurface(id_surface, surface, isRecordable, callback);
} else {
Log.e(TAG, "failed to get CameraServer:serviceID=" + serviceID);
}
}
@Override
public void removeSurface(final int serviceID, final int id_surface) throws RemoteException {
if (DEBUG) Log.d(TAG, "mSlaveBinder#removeSurface:id=" + id_surface);
final CameraServer server = getCameraServer(serviceID);
if (server != null) {
server.removeSurface(id_surface);
} else {
Log.e(TAG, "failed to get CameraServer:serviceID=" + serviceID);
}
}
};
最佳答案
您可能应该使用一些 try catch 语句来确保您的程序不会因为 Uncaught Error 而崩溃:
@Override
public void onDettach(final UsbDevice device) {
if (DEBUG) Log.v(TAG, "OnDeviceConnectListener#onDettach:");
try {
if (mCameraClient != null) {
mCameraClient.disconnect();
mCameraClient.release();
mCameraClient = null;
}
updateCameraDialog();
} catch (Exception ex) {
Log.e("Exception occurred", ex);
}
}
通常捕获所有异常是不好的做法,因此您应该检查抛出的异常并将捕获的异常更改为该特定异常。
关于android - 如何使用 USB 网络摄像头修复 Android 中的拔出 USB 电缆错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35239658/
Rails 5 引入了 Action Cable,但在 Rails 4 中有实时流媒体。所以我很困惑两者有什么区别,Live Streaming 在 rails 5 中已经死了吗?或 Action C
我有一个很奇怪的问题。最近我尝试使用以下代码在我的应用程序中使用唤醒锁: /** Called when the activity is first created. */ @Override
我在我的应用程序中设置了一个基本的行动电缆供电聊天。我有一个 after_create_commit 回调,它将消息发送到要广播到适当 channel 的作业。当它设置为 perform_now 时它
我在使用 Windows Phone 7.5 的 HTC Titan 上运行一个非常简单的 TCP 客户端时遇到了一些麻烦。 当 USB 数据线连接到手机上时,TCP 客户端工作得很好,但是一旦拔下数
您好,我已经使用 rails 5 实现了 action cable。它在本地工作正常,但在 heroku 上不工作。在 heroku 上,每当我发送任何消息时,它都会发送四条消息,两条重复消息和两条空
在聊天中传递错过的消息的好方法是什么,例如使用 Action Cable?就像当您关闭笔记本电脑并再次打开它时,您并没有重新加载页面,但您可能错过了消息 一种方法是使用 Action Cable 进行
我正在尝试在 websocket 连接上实现授权(rails 5.2.1) 按照 rubyonrails guideline ,我创建了connection.rb如下: # app/channels/
我正在尝试使 kable 表具有反应性并将其导出到 Shiny 的应用程序中。已经尝试在服务器内使用 renderDataTable/renderTable 并将输出函数作为 datatableOut
是否可以将 USB 打印机与 Epson ePos Javascript SDK 一起使用?还是仅适用于以太网/无线型号? 我希望找到一个实用程序将 USB 打印机转换为网络监听打印机,以回收我所有的
我正在使用 ActionCable,并在每 3 秒间隔后从服务器接收 ping(在 ActionCable 库中提到)。我的问题是:如何更改订阅时的 ping 间隔? 有什么想法吗? 最佳答案 扩展@
我正在尝试使用 ISO 9141-2 协议(protocol)从 ECU 读取数据。我使用的电缆是OBD2转USB,使用FT232R芯片。我正在运行的程序是用 C 编写的。 当我向串行端口(ttyUS
我是一名优秀的程序员,十分优秀!