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