|
|
|
package com.jiangdg.usbcamera.view;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.hardware.usb.UsbDevice;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Looper;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.v7.app.ActionBar;
|
|
|
|
import android.support.v7.app.AlertDialog;
|
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.support.v7.widget.Toolbar;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.Surface;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.CompoundButton;
|
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.SeekBar;
|
|
|
|
import android.widget.SimpleAdapter;
|
|
|
|
import android.widget.Switch;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import com.jiangdg.usbcamera.R;
|
|
|
|
import com.jiangdg.usbcamera.UVCCameraHelper;
|
|
|
|
import com.jiangdg.usbcamera.utils.FileUtils;
|
|
|
|
import com.serenegiant.usb.CameraDialog;
|
|
|
|
import com.serenegiant.usb.Size;
|
|
|
|
import com.serenegiant.usb.USBMonitor;
|
|
|
|
import com.serenegiant.usb.common.AbstractUVCCameraHandler;
|
|
|
|
import com.serenegiant.usb.encoder.RecordParams;
|
|
|
|
import com.serenegiant.usb.widget.CameraViewInterface;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* UVCCamera use demo
|
|
|
|
* <p>
|
|
|
|
* Created by jiangdongguo on 2017/9/30.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class USBCameraActivity extends AppCompatActivity implements CameraDialog.CameraDialogParent, CameraViewInterface.Callback {
|
|
|
|
private static final String TAG = "Debug";
|
|
|
|
@BindView(R.id.camera_view)
|
|
|
|
public View mTextureView;
|
|
|
|
@BindView(R.id.toolbar)
|
|
|
|
public Toolbar mToolbar;
|
|
|
|
@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;
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_usbcamera);
|
|
|
|
ButterKnife.bind(this);
|
|
|
|
initView();
|
|
|
|
|
|
|
|
// step.1 initialize UVCCameraHelper
|
|
|
|
mUVCCameraView = (CameraViewInterface) mTextureView;
|
|
|
|
mUVCCameraView.setCallback(this);
|
|
|
|
mCameraHelper = UVCCameraHelper.getInstance();
|
|
|
|
mCameraHelper.initUSBMonitor(this, mUVCCameraView, listener);
|
|
|
|
|
|
|
|
|
|
|
|
mCameraHelper.setOnPreviewFrameListener(new AbstractUVCCameraHandler.OnPreViewResultListener() {
|
|
|
|
@Override
|
|
|
|
public void onPreviewResult(byte[] nv21Yuv) {
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initView() {
|
|
|
|
setSupportActionBar(mToolbar);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.menu_takepic:
|
|
|
|
if (mCameraHelper == null || !mCameraHelper.isCameraOpened()) {
|
|
|
|
showShortMsg("sorry,camera open failed");
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
String picPath = UVCCameraHelper.ROOT_PATH + 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.menu_recording:
|
|
|
|
if (mCameraHelper == null || !mCameraHelper.isCameraOpened()) {
|
|
|
|
showShortMsg("sorry,camera open failed");
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
if (!mCameraHelper.isRecording()) {
|
|
|
|
String videoPath = UVCCameraHelper.ROOT_PATH + System.currentTimeMillis();
|
|
|
|
// FileUtils.createfile(FileUtils.ROOT_PATH + "test666.h264");
|
|
|
|
RecordParams params = new RecordParams();
|
|
|
|
params.setRecordPath(videoPath);
|
|
|
|
params.setRecordDuration(0); // 设置为0,不分割保存
|
|
|
|
params.setVoiceClose(mSwitchVoice.isChecked()); // is close voice
|
|
|
|
mCameraHelper.startRecording(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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
showShortMsg("start record...");
|
|
|
|
mSwitchVoice.setEnabled(false);
|
|
|
|
} else {
|
|
|
|
// FileUtils.releaseFile();
|
|
|
|
mCameraHelper.stopRecording();
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
// example: {640x480,320x240,etc}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
FileUtils.releaseFile();
|
|
|
|
// step.4 release uvc camera resources
|
|
|
|
if (mCameraHelper != null) {
|
|
|
|
mCameraHelper.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|