Merge pull request #125 from lacerta-doc/improve_scanner2

Improve scanner2
This commit is contained in:
ろむねこ 2024-01-29 04:33:12 +09:00 committed by GitHub
commit a3b3000d59
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 87 additions and 16 deletions

View File

@ -52,4 +52,6 @@ dependencies {
implementation project(':vcs')
implementation project(':data')
implementation project(':component:common')
}

View File

@ -2,6 +2,7 @@ package one.nem.lacerta.component.scanner;
import android.Manifest;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
@ -29,7 +30,9 @@ import java.util.concurrent.CompletableFuture;
import javax.inject.Inject;
import dagger.hilt.android.AndroidEntryPoint;
import one.nem.lacerta.component.common.picker.LacertaFilePickerDialog;
import one.nem.lacerta.data.Document;
import one.nem.lacerta.data.LacertaLibrary;
import one.nem.lacerta.model.document.DocumentDetail;
import one.nem.lacerta.model.document.DocumentMeta;
import one.nem.lacerta.processor.factory.DocumentProcessorFactory;
@ -53,9 +56,25 @@ public class ScannerManagerActivity extends AppCompatActivity {
@Inject
LacertaVcsFactory lacertaVcsFactory;
// @Inject
// public ScannerManagerActivity(LacertaLogger logger, Document document, LacertaLibrary lacertaLibrary, DocumentProcessorFactory documentProcessorFactory, LacertaVcsFactory lacertaVcsFactory) {
// this.logger = logger;
// this.document = document;
// this.lacertaLibrary = lacertaLibrary;
// this.documentProcessorFactory = documentProcessorFactory;
// this.lacertaVcsFactory = lacertaVcsFactory;
// }
@Inject
public ScannerManagerActivity() {
// Required empty public constructor
}
// Variables
private ArrayList<Bitmap> croppedImages = new ArrayList<>();
private boolean single = false;
View view;
DocumentScanner documentScanner = new DocumentScanner(
@ -85,6 +104,36 @@ public class ScannerManagerActivity extends AppCompatActivity {
null
);
DocumentScanner documentScannerSingle = new DocumentScanner( // TODO-rca: ひどすぎるのでなんとかする
this,
(croppedImageResults) -> {
logger.debug(TAG, "croppedImage size: " + croppedImageResults.size());
ArrayList<Bitmap> croppedImages = new ArrayList<>();
for (String result : croppedImageResults) {
croppedImages.add(BitmapFactory.decodeFile(result));
}
processResult(croppedImages);
return null;
},
(errorMessage) -> {
// an error happened
logger.error(TAG, "Error: " + errorMessage);
logger.e_code("543a230e-cb9a-47a2-8131-3beecfe1c458");
finish();
return null;
},
() -> {
// user canceled document scan
logger.debug(TAG, "User canceled document scan");
finish();
return null;
},
null,
null,
1
);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -108,11 +157,21 @@ public class ScannerManagerActivity extends AppCompatActivity {
setSupportActionBar(toolbar);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle != null) {
this.single = bundle.getBoolean("single", false);
}
if (this.single) {
documentScanner = documentScannerSingle;
}
documentScanner.startScan();
// Init
this.view = findViewById(R.id.main); // TODO-rca:なんとかする
}
@Override
@ -125,15 +184,13 @@ public class ScannerManagerActivity extends AppCompatActivity {
public boolean onOptionsItemSelected(android.view.MenuItem item) {
if (item.getItemId() == R.id.action_save_new) {
// 新ドキュメントとして保存
Toast.makeText(this, "保存処理", Toast.LENGTH_SHORT).show();
saveNewDocument();
return true;
// }
// else if (item.getItemId() == R.id.action_insert_exist) {
// // 既存ドキュメントに挿入
// Toast.makeText(this, "Work in progress", Toast.LENGTH_SHORT).show();
// insertToExistDocument();
// return true;
}
else if (item.getItemId() == R.id.action_insert_exist) {
// 既存ドキュメントに挿入
insertToExistDocument();
return true;
} else if (item.getItemId() == android.R.id.home) {
finish();
return true;
@ -174,7 +231,7 @@ public class ScannerManagerActivity extends AppCompatActivity {
Bitmap[] bitmaps = new Bitmap[croppedImages.size()];
croppedImages.toArray(bitmaps);
logger.debug(TAG, "bitmaps.length: " + bitmaps.length);
addPagesToDocumentDetail(documentDetail, bitmaps).join();
addPagesToDocumentDetail(documentDetail, bitmaps, null).join();
document.updateDocument(documentDetail).join();
dialog.dismiss();
finish();
@ -182,11 +239,11 @@ public class ScannerManagerActivity extends AppCompatActivity {
}
private CompletableFuture<Void> addPagesToDocumentDetail(DocumentDetail documentDetail, Bitmap[] bitmaps) {
private CompletableFuture<Void> addPagesToDocumentDetail(DocumentDetail documentDetail, Bitmap[] bitmaps, String commitMessage) {
return CompletableFuture.runAsync(() -> {
try {
document.updateDocument(documentProcessorFactory.create(documentDetail).addNewPagesToLast(bitmaps).getDocumentDetail()).join();
lacertaVcsFactory.create(documentDetail.getMeta().getId()).generateRevisionAtCurrent("Initial commit");
lacertaVcsFactory.create(documentDetail.getMeta().getId()).generateRevisionAtCurrent(commitMessage == null ? "Update" : commitMessage);
} catch (Exception e) {
logger.error(TAG, "Error: " + e.getMessage());
logger.e_code("9dff2a28-20e8-4ccd-9d04-f0c7646faa6a");
@ -196,7 +253,19 @@ public class ScannerManagerActivity extends AppCompatActivity {
private void insertToExistDocument() {
logger.debug(TAG, "insertToExistDocument");
// TODO-rca: 実装
LacertaFilePickerDialog dialog = new LacertaFilePickerDialog();
dialog.setListener(((name, fileId) -> {
document.getDocument(fileId).thenAccept((documentDetail) -> {
Bitmap[] bitmaps = new Bitmap[croppedImages.size()];
croppedImages.toArray(bitmaps);
logger.debug(TAG, "bitmaps.length: " + bitmaps.length);
addPagesToDocumentDetail(documentDetail, bitmaps, "ページを追加").join();
document.updateDocument(documentDetail).join();
finish();
});
}));
dialog.setTitle("追加するドキュメントを選択");
dialog.show(getSupportFragmentManager(), "LacertaFilePickerDialog");
}
private void updateResultView(ArrayList<Bitmap> resultImages) {

View File

@ -6,9 +6,9 @@
android:icon="@drawable/save_24px"
android:title="Save"
app:showAsAction="ifRoom"/>
<!-- <item-->
<!-- android:id="@+id/action_insert_exist"-->
<!-- android:icon="@drawable/description_24px"-->
<!-- android:title="Insert"-->
<!-- app:showAsAction="ifRoom"/>-->
<item
android:id="@+id/action_insert_exist"
android:icon="@drawable/description_24px"
android:title="Insert"
app:showAsAction="ifRoom"/>
</menu>