Skip to content

Commit

Permalink
update to androidx and supporting overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangdongguo committed Jan 15, 2020
1 parent 2980805 commit e405a16
Show file tree
Hide file tree
Showing 22 changed files with 325 additions and 24 deletions.
26 changes: 11 additions & 15 deletions app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.os.Handler;
import android.os.Looper;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
Expand All @@ -17,13 +18,12 @@
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.Toast;

import com.jiangdg.usbcamera.R;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.jiangdg.usbcamera.R;
import com.jiangdg.usbcamera.UVCCameraHelper;
import com.jiangdg.usbcamera.application.MyApplication;
import com.jiangdg.usbcamera.utils.FileUtils;
Expand All @@ -34,6 +34,7 @@
import com.serenegiant.usb.encoder.RecordParams;
import com.serenegiant.usb.widget.CameraViewInterface;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -138,7 +139,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
mCameraHelper.setDefaultFrameFormat(UVCCameraHelper.FRAME_FORMAT_MJPEG);
mCameraHelper.initUSBMonitor(this, mUVCCameraView, listener);


mCameraHelper.setOnPreviewFrameListener(new AbstractUVCCameraHandler.OnPreViewResultListener() {
@Override
public void onPreviewResult(byte[] nv21Yuv) {
Expand Down Expand Up @@ -222,13 +222,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
showShortMsg("sorry,camera open failed");
return super.onOptionsItemSelected(item);
}
String picPath = UVCCameraHelper.ROOT_PATH + MyApplication.DIRECTORY_NAME +"/images";
String picFilename = System.currentTimeMillis() + UVCCameraHelper.SUFFIX_JPEG;

File picPathFolder = new File(picPath);
if (!picPathFolder.exists()) picPathFolder.mkdirs();
String picPath = UVCCameraHelper.ROOT_PATH + MyApplication.DIRECTORY_NAME +"/images/"
+ System.currentTimeMillis() + UVCCameraHelper.SUFFIX_JPEG;

mCameraHelper.capturePicture(picPath+'/'+picFilename, new AbstractUVCCameraHandler.OnCaptureListener() {
mCameraHelper.capturePicture(picPath, new AbstractUVCCameraHandler.OnCaptureListener() {
@Override
public void onCaptureResult(String path) {
if(TextUtils.isEmpty(path)) {
Expand All @@ -250,17 +247,16 @@ public void run() {
return super.onOptionsItemSelected(item);
}
if (!mCameraHelper.isPushing()) {
String videoPath = UVCCameraHelper.ROOT_PATH + MyApplication.DIRECTORY_NAME +"/videos";
String videoFilename = String.valueOf(System.currentTimeMillis());
File videoPathFolder = new File(videoPath);
if (!videoPathFolder.exists()) videoPathFolder.mkdirs();
String videoPath = UVCCameraHelper.ROOT_PATH + MyApplication.DIRECTORY_NAME +"/videos/" + System.currentTimeMillis()
+ UVCCameraHelper.SUFFIX_MP4;

FileUtils.createfile(FileUtils.ROOT_PATH + "test666.h264");
// FileUtils.createfile(FileUtils.ROOT_PATH + "test666.h264");
// if you want to record,please create RecordParams like this
RecordParams params = new RecordParams();
params.setRecordPath(videoPath);
params.setRecordDuration(0); // 设置为0,不分割保存(单位为分钟)
params.setRecordDuration(0); // auto divide saved,default 0 means not divided
params.setVoiceClose(mSwitchVoice.isChecked()); // is close voice
params.setSupportOverlay(true); // overlay only support armeabi-v7a & arm64-v8a
mCameraHelper.startPusher(params, new AbstractUVCCameraHandler.OnEncodeResultListener() {
@Override
public void onEncodeResult(byte[] data, int offset, int length, long timestamp, int type) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_splash.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="last modify 2019.6"
android:text="last modify 2020.01"
android:textSize="16sp"
android:textStyle="bold"
android:gravity="bottom|center_horizontal"
Expand Down
6 changes: 6 additions & 0 deletions libusbcamera/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ android {
versionCode 2
versionName rootProject.ext.versionNameString
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

// "armeabi-v7a" can adapter most of the devices
ndk {
abiFilters "armeabi-v7a"
}
}
buildTypes {
release {
Expand All @@ -30,4 +35,5 @@ dependencies {

implementation "androidx.appcompat:appcompat:${androidXVersion}"
api "com.serenegiant:common:${commonLibVersion}"
implementation project(path: ':libutils')
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
import com.serenegiant.usb.encoder.RecordParams;
import com.serenegiant.usb.widget.CameraViewInterface;

import org.easydarwin.sw.TxtOverlay;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Objects;

/** UVCCamera Helper class
*
Expand Down Expand Up @@ -80,6 +83,7 @@ public interface OnMyDevConnectListener {
public void initUSBMonitor(Activity activity, CameraViewInterface cameraView, final OnMyDevConnectListener listener) {
this.mActivity = activity;
this.mCamView = cameraView;

mUSBMonitor = new USBMonitor(activity.getApplicationContext(), new USBMonitor.OnDeviceConnectListener() {

// called by checking usb device
Expand Down Expand Up @@ -246,8 +250,9 @@ public List<UsbDevice> getUsbDeviceList() {

public void capturePicture(String savePath,AbstractUVCCameraHandler.OnCaptureListener listener) {
if (mCameraHandler != null && mCameraHandler.isOpened()) {

File file = new File(savePath);
if(! file.getParentFile().exists()) {
if(! Objects.requireNonNull(file.getParentFile()).exists()) {
file.getParentFile().mkdirs();
}
mCameraHandler.captureStill(savePath,listener);
Expand All @@ -262,6 +267,9 @@ public void startPusher(AbstractUVCCameraHandler.OnEncodeResultListener listener

public void startPusher(RecordParams params, AbstractUVCCameraHandler.OnEncodeResultListener listener) {
if (mCameraHandler != null && !isPushing()) {
if(params.isSupportOverlay()) {
TxtOverlay.install(mActivity.getApplicationContext());
}
mCameraHandler.startRecording(params, listener);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import com.serenegiant.usb.encoder.biz.Mp4MediaMuxer;
import com.serenegiant.usb.widget.CameraViewInterface;

import org.easydarwin.sw.TxtOverlay;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
Expand All @@ -45,7 +47,11 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

Expand Down Expand Up @@ -413,6 +419,7 @@ public static final class CameraThread extends Thread {
private Mp4MediaMuxer mMuxer;
private boolean isPushing;
private String videoPath;
private boolean isSupportOverlay;
// private boolean isAudioThreadStart;

/**
Expand Down Expand Up @@ -667,11 +674,17 @@ public void handleStartPusher(RecordParams params) {
return;
// // 获取USB Camera预览数据
// mUVCCamera.setFrameCallback(mIFrameCallback, UVCCamera.PIXEL_FORMAT_NV21);

// 初始化混合器
if (params != null) {
isSupportOverlay = params.isSupportOverlay();
if(isSupportOverlay) {
// init overlay engine
TxtOverlay.getInstance().init(mWidth, mHeight);
}
videoPath = params.getRecordPath();
File file = new File(videoPath);
if(! file.getParentFile().exists()) {
if(! Objects.requireNonNull(file.getParentFile()).exists()) {
file.getParentFile().mkdirs();
}
mMuxer = new Mp4MediaMuxer(params.getRecordPath(),
Expand All @@ -697,6 +710,8 @@ public void handleStopPusher() {
// 停止音视频编码线程
stopAudioRecord();
stopVideoRecord();
if(isSupportOverlay)
TxtOverlay.getInstance().release();
// // 停止捕获视频数据
// if (mUVCCamera != null) {
// mUVCCamera.stopCapture();
Expand Down Expand Up @@ -808,7 +823,7 @@ public void onFrame(final ByteBuffer frame) {
if (mPreviewListener != null) {
mPreviewListener.onPreviewResult(yuv);
}
// 捕获图片
// picture
if (isCaptureStill && !TextUtils.isEmpty(picPath)) {
isCaptureStill = false;
new Thread(new Runnable() {
Expand All @@ -818,9 +833,13 @@ public void run() {
}
}).start();
}
// 视频
// video
if (mH264Consumer != null) {
// 修改分辨率参数
// overlay
if(isSupportOverlay) {
TxtOverlay.getInstance().overlay(yuv, new SimpleDateFormat("yyyy-MM-dd EEEE HH:mm:ss").format(new Date()));
}

mH264Consumer.setRawYuv(yuv, mWidth, mHeight);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,22 @@
*/

public class RecordParams {
private String recordPath; // 保存路径
private int recordDuration; // 分钟
private String recordPath;
private int recordDuration;
private boolean voiceClose;
private boolean isAutoSave;
private boolean isSupportOverlay;

public RecordParams() {
}

public boolean isSupportOverlay() {
return isSupportOverlay;
}

public void setSupportOverlay(boolean supportOverlay) {
isSupportOverlay = supportOverlay;
}

public boolean isVoiceClose() {
return voiceClose;
Expand Down
1 change: 1 addition & 0 deletions libutils/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
34 changes: 34 additions & 0 deletions libutils/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 28
buildToolsVersion "29.0.2"


defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Empty file added libutils/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions libutils/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.easydarwin.sw;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

assertEquals("org.easydarwin.sw.test", appContext.getPackageName());
}
}
2 changes: 2 additions & 0 deletions libutils/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.easydarwin.sw" />
Binary file added libutils/src/main/assets/zk/SIMYOU.ttf
Binary file not shown.
Loading

0 comments on commit e405a16

Please sign in to comment.