feat: 增加0.5秒一张的拍照

This commit is contained in:
xiaoyan 2022-06-15 11:34:53 +08:00
parent 2e00c11735
commit c6ca30eb0f
3 changed files with 114 additions and 60 deletions

View File

@ -122,7 +122,7 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
private SystemTTS systemTTS;
private StringBuilder picturesBuilder;
private LatLng startLatLine, endLatLine;
private TextView tvTitle;
private TextView tvTitle, tvConvert/*照片已转换为webp的张数*/;
private GPSUtils gpsUtils;
private Location gpsLocation;
private boolean isMapSlide = false;
@ -133,6 +133,7 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
private int type = 0;
private int radioPicture = 0;
private int videoIndex = -1;
private int convertIndex = 0;
private int startVideoIndex = -1;
@SuppressLint("SimpleDateFormat")
private SimpleDateFormat formatter;
@ -140,6 +141,7 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
@Override
public boolean handleMessage(@NonNull Message msg) {
if (msg.what == 0x101) {
System.out.println("收到拍照按钮请求");
camera.takePicture();
} else if (msg.what == 0x102) {
if (imageView != null) {
@ -151,6 +153,8 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
}
capturePicture.setChecked(false);
stopTimer();
} else if (msg.what == 0x104) {
tvConvert.setText("转换成功:"+(++msg.arg1));
}
return false;
}
@ -182,6 +186,7 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
} else {
videoIndex = videoIndex - 1;
}
convertIndex = videoIndex;
}
}
formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
@ -204,22 +209,17 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
});
RadioGroup radioGroupPicture = findViewById(R.id.radio_group_picture);
RadioButton radioBtnHand = findViewById(R.id.radio_btn_hand);
RadioButton radioBtnHalfSec = findViewById(R.id.radio_btn_half_sec);
RadioButton radioBtnAuto = findViewById(R.id.radio_btn_auto);
RadioButton radioBtnAutoSec = findViewById(R.id.radio_btn_auto_sec);
if (type == 3) {//poiVideo 1秒
ivPicVideoImage.setVisibility(View.VISIBLE);
ivPicRoadImage.setVisibility(View.GONE);
radioPicture = 3;
radioBtnAutoSec.setTextColor(Color.parseColor("#FFEB3B"));
radioBtnHand.setTextColor(Color.WHITE);
radioBtnAuto.setTextColor(Color.WHITE);
} else if (type == 4) {//道路2秒
ivPicRoadImage.setVisibility(View.VISIBLE);
ivPicVideoImage.setVisibility(View.GONE);
radioPicture = 3;
radioBtnAutoSec.setTextColor(Color.parseColor("#FFEB3B"));
radioBtnAuto.setTextColor(Color.WHITE);
radioBtnHand.setTextColor(Color.WHITE);
}
radioGroupPicture.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
@ -230,27 +230,24 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
isOration = false;
capturePicture.setText("拍摄");
capturePicture.setChecked(false);
radioBtnHand.setTextColor(Color.parseColor("#FFEB3B"));
radioBtnAuto.setTextColor(Color.WHITE);
radioBtnAutoSec.setTextColor(Color.WHITE);
stopTimer();
break;
case R.id.radio_btn_auto://自动1秒:
radioPicture = 2;
capturePicture.setText("开始采集");
capturePicture.setChecked(false);
radioBtnAuto.setTextColor(Color.parseColor("#FFEB3B"));
radioBtnHand.setTextColor(Color.WHITE);
radioBtnAutoSec.setTextColor(Color.WHITE);
stopTimer();
break;
case R.id.radio_btn_auto_sec://自动2
radioPicture = 3;
capturePicture.setText("开始采集");
capturePicture.setChecked(false);
radioBtnAutoSec.setTextColor(Color.parseColor("#FFEB3B"));
radioBtnAuto.setTextColor(Color.WHITE);
radioBtnHand.setTextColor(Color.WHITE);
stopTimer();
break;
case R.id.radio_btn_half_sec://自动0.5
radioPicture = 4;
capturePicture.setText("开始采集");
capturePicture.setChecked(false);
stopTimer();
break;
}
@ -267,6 +264,7 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
camera = findViewById(R.id.camera);
camera.setOnClickListener(this);
tvTitle = findViewById(R.id.tv_title);
tvConvert = findViewById(R.id.tv_convert);
imageView = findViewById(R.id.image_view);
imageView.setOnClickListener(this);
capturePicture = findViewById(R.id.capture_picture);
@ -311,8 +309,9 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
camera.addCameraListener(new CameraListener() {
@Override
public void onPictureTaken(@NonNull PictureResult result) {
if (result.getData() != null && result != null && result.getData().length > 0) {
if (result != null && result.getData() != null && result.getData().length > 0) {
super.onPictureTaken(result);
System.out.println("收到拍照按钮jieguo");
isBack = true;
// 如果当前手机是竖向则不
if (isOration) {
@ -331,13 +330,22 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
isOration = false;
}
}
File file = new File(finalVideoPath);
result.toFile(file, new FileCallback() {
@Override
public void onFileReady(@Nullable File file) {
UserApplication.fixedThreadPool.execute(new Jpg2WebpRunnable(/*result, */file, 0, booleanExtra));
}
});
synchronized (finalVideoPath) {
// 生成点位marker
initMarker(booleanExtra);
int currentIndex = videoIndex = Integer.parseInt(file.getName().replace(".webp", ""));
// 下一张照片的路径
finalVideoPath = Objects.requireNonNull(file.getParentFile()).getAbsolutePath() + "/" + (videoIndex + 1) + ".webp";
tvTitle.setText("拍摄成功:" + (videoIndex + 1));
result.toFile(file, new FileCallback() {
@Override
public void onFileReady(@Nullable File file) {
UserApplication.fixedThreadPool.execute(new Jpg2WebpRunnable(/*result, */file, 0, booleanExtra, currentIndex));
}
});
}
} else {
isBack = false;
if (isOration) {
@ -373,41 +381,42 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
class Jpg2WebpRunnable implements Runnable {
//private PictureResult pictureResult;
private File file;
private int count;
private int count;/*线程重复执行的次数*/
private boolean isBoolean;
private int index;
// 该转换执行次数如果连续3次执行失败则不再转换
public Jpg2WebpRunnable(/*PictureResult pictureResult,*/ File file, int count, boolean isBoolean) {
public Jpg2WebpRunnable(/*PictureResult pictureResult,*/ File file, int count, boolean isBoolean, int index/*照片的索引*/) {
//this.pictureResult = pictureResult;
this.file = file;
this.count = count;
this.isBoolean = isBoolean;
this.index = index;
}
@Override
public void run() {
if (file.exists() && file != null) {
initWeb(file, count, isBoolean);
if (count <= 0) { // 不是重新转换webp流程
runOnUiThread(new Runnable() {
@SuppressLint("SetTextI18n")
@Override
public void run() {
if (PicturesActivity.this != null) {
if (file.exists()) {
initMarker(isBoolean);
videoIndex = Integer.parseInt(file.getName().replace(".webp", ""));
finalVideoPath = Objects.requireNonNull(file.getParentFile()).getAbsolutePath() + "/" + (videoIndex + 1) + ".webp";
tvTitle.setText("保存成功:" + (videoIndex + 1));
}
if (radioPicture == 1 && handler != null) {
Message message = new Message();
message.what = 0x103;
handler.sendMessage(message);
}
}
if (initWeb(file, count, isBoolean, index)) {
if (PicturesActivity.this != null&&handler != null) {
if (radioPicture == 1) {
Message message = new Message();
message.what = 0x103;
handler.sendMessage(message);
} else {
Message message = new Message();
message.what = 0x104;
message.arg1 = index;
handler.sendMessage(message);
}
});
}
// runOnUiThread(new Runnable() {
// @SuppressLint("SetTextI18n")
// @Override
// public void run() {
//
// }
// });
}
} else {
isBack = false;
@ -428,7 +437,7 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
}
}
private void initWeb(File file, int count, boolean isBoolean) {
private boolean initWeb(File file, int count, boolean isBoolean, int index) {
try {
count++;
WebPNative webPNative = new WebPNative();
@ -437,7 +446,8 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
if (!bitmap.isRecycled()) {
bitmap.recycle();
}
initMarkerPaper();
initMarkerPaper(index);
return true;
} catch (RuntimeException e) {
e.printStackTrace();
//如果是写入txt记录失败上传失败记录
@ -447,9 +457,10 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
UMCrashManager.reportCrash(this, e);
if (count < 3) {
//当尝试次数小于3次则加入转换队列尝试重新转换
UserApplication.fixedThreadPool.execute(new Jpg2WebpRunnable(/*result, */file, count, isBoolean));
UserApplication.fixedThreadPool.execute(new Jpg2WebpRunnable(/*result, */file, count, isBoolean, index));
}
}
return false;
}
private void initLine() {
@ -782,8 +793,8 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
}
@SuppressLint("SetTextI18n")
public void initMarkerPaper() {
int endVideoIndex = videoIndex == -1 ? 0 : (videoIndex + 1);
public void initMarkerPaper(int index) {
int endVideoIndex = index == -1 ? 0 : (index + 1);
if (startVideoIndex == endVideoIndex) {
return;
}
@ -792,8 +803,8 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
StringBuilder sb = new StringBuilder();
sb.append(formatter.format(new Date())); // 记录当前时
sb.append(",");
sb.append(videoIndex == -1 ? 0 : (videoIndex + 1));//個數
startVideoIndex = videoIndex == -1 ? 0 : (videoIndex + 1);
sb.append(index == -1 ? 0 : (index + 1));//個數
startVideoIndex = index == -1 ? 0 : (index + 1);
sb.append(",");
sb.append(Constant.currentLocation.getLatitude());
sb.append(",");
@ -990,6 +1001,8 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
timer.schedule(timerTask, 0, 1000);
} else if (radioPicture == 3) {
timer.schedule(timerTask, 0, 2000);
} else if (radioPicture == 4) {
timer.schedule(timerTask, 0, 500);
}
}

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#FFEB3B"/> <!-- pressed 看下的颜色-->
<item android:state_focused="true"
android:color="#FFEB3B"/> <!-- focused -->
<item android:state_selected="true"
android:color="#FFEB3B"/> <!-- focused -->
<item android:state_checked="true"
android:color="#FFEB3B"/> <!-- focused -->
<item android:state_enabled="false"
android:color="@color/colorGrays"/> <!-- focused -->
<item android:color="@color/white"/> <!-- default 默认颜色 -->
</selector>

View File

@ -61,15 +61,32 @@
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.4" />
<TextView
android:id="@+id/tv_title"
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_convert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
<ImageView
android:id="@+id/iv_zoom_add"
@ -144,7 +161,17 @@
android:layout_marginBottom="15dp"
android:button="@null"
android:text="手动"
android:textColor="@color/white" />
android:textColor="@color/rbtn_text_color_selector" />
<RadioButton
android:id="@+id/radio_btn_half_sec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:button="@null"
android:text="自动0.5秒"
android:textColor="@color/rbtn_text_color_selector" />
<RadioButton
android:id="@+id/radio_btn_auto"
@ -154,7 +181,7 @@
android:layout_marginBottom="15dp"
android:button="@null"
android:text="自动1秒"
android:textColor="@color/white" />
android:textColor="@color/rbtn_text_color_selector" />
<RadioButton
android:id="@+id/radio_btn_auto_sec"
@ -164,7 +191,7 @@
android:layout_marginBottom="15dp"
android:button="@null"
android:text="自动2秒"
android:textColor="@color/white" />
android:textColor="@color/rbtn_text_color_selector" />
</RadioGroup>