Use DataMatrix codes, they are much faster. Start work on storing settings

This commit is contained in:
Astatin3
2024-05-16 10:45:44 -06:00
parent 2a37252386
commit 2e237630c4
9 changed files with 102 additions and 22 deletions
@@ -0,0 +1,51 @@
package com.astatin3.scoutingapp2025.SettingsVersionStack;
import com.astatin3.scoutingapp2025.fileEditor;
import java.nio.charset.StandardCharsets;
public abstract class settingsVersion {
private static String settingsFilename = "settings.txt";
public abstract int getVersion();
public abstract void update();
public static String readLine(int line){
if(!fileEditor.fileExist("settings.txt")){return null;}
String[] fileContent = new String(fileEditor.readFile("settings.txt"), StandardCharsets.UTF_8).split("\n");
if(fileContent.length <= line){return null;}
return fileContent[line];
}
public static String w riteLine(int line, String data){
String[] fileContent;
if(!fileEditor.fileExist(settingsFilename)){
fileContent = new String[]{};
}else{
fileContent = new String(fileEditor.readFile(settingsFilename), StandardCharsets.UTF_8).split("\n");
}
String newFile = "";
for(int i = 0; i < Math.max(fileContent.length-1, line); i++){
if(i == line) {
newFile += data + "\n";
}else if(i > fileContent.length-1){
newFile += fileContent[i];
}
if(i < Math.max(fileContent.length - 1, line) - 1){
newFile += "\n";
}
}
return newFile;
// fileEditor.writeFile(settingsFilename, newFile.getBytes(StandardCharsets.UTF_8));
}
}
@@ -0,0 +1,12 @@
package com.astatin3.scoutingapp2025.SettingsVersionStack;
public class v0 extends settingsVersion {
@Override
public int getVersion() {
return 0;
}
public void update(){
}
}
@@ -12,7 +12,11 @@ import com.google.zxing.NotFoundException;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.aztec.AztecReader;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.datamatrix.DataMatrixReader;
import com.google.zxing.maxicode.MaxiCodeReader;
import com.google.zxing.pdf417.PDF417Reader;
import com.google.zxing.qrcode.QRCodeReader;
import java.util.EnumSet;
@@ -20,7 +24,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
public class qrScanTask extends AsyncTask<String, String, String>{
public class codeScanTask extends AsyncTask<String, String, String>{
private Function<String, String> resultFunction = null;
private Bitmap image;
@@ -39,9 +43,9 @@ public class qrScanTask extends AsyncTask<String, String, String>{
Map<DecodeHintType, Object> hints = new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
// hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
hints.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.of(BarcodeFormat.QR_CODE));
hints.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.of(BarcodeFormat.DATA_MATRIX));
Reader reader = new QRCodeReader();
Reader reader = new DataMatrixReader();
try {
Result result = reader.decode(binaryBitmap, hints);
return result.getText();
@@ -3,7 +3,6 @@ package com.astatin3.scoutingapp2025;
import android.content.Context;
import com.astatin3.scoutingapp2025.types.frcEvent;
import com.astatin3.scoutingapp2025.types.frcMatch;
import com.astatin3.scoutingapp2025.types.frcTeam;
import java.io.BufferedInputStream;
@@ -13,7 +12,6 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.BufferOverflowException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
@@ -44,7 +42,7 @@ public final class fileEditor {
public static char byteToChar(int num){
return new String(toBytes(num, 1), StandardCharsets.UTF_8).charAt(0);
return new String(toBytes(num, 1), StandardCharsets.ISO_8859_1).charAt(0);
}
@@ -125,7 +123,7 @@ public final class fileEditor {
return outputStream.toByteArray();
}
private static boolean writeToFile(String filepath, byte[] data) {
public static boolean writeFile(String filepath, byte[] data) {
try {
FileOutputStream output = new FileOutputStream(filepath);
output.write(data);
@@ -138,8 +136,13 @@ public final class fileEditor {
}
}
public static boolean fileExist(String path){
File f = new File(baseDir + path);
return f.exists() && !f.isDirectory();
}
public static byte[] readFile(String path){
File file = new File(path);
File file = new File(baseDir + path);
int size = (int) file.length();
byte[] bytes = new byte[size];
try {
@@ -176,7 +179,7 @@ public final class fileEditor {
public static boolean setEvent(frcEvent event){
final String filename = (baseDir + event.eventCode + ".eventdata");
return writeToFile(filename, event.encode());
return writeFile(filename, event.encode());
}
public static ArrayList<String> getEventList(){
@@ -13,6 +13,8 @@ import androidx.fragment.app.Fragment;
import com.astatin3.scoutingapp2025.databinding.FragmentSettingsBinding;
import com.astatin3.scoutingapp2025.fileEditor;
import com.astatin3.scoutingapp2025.SettingsVersionStack.v0;
import com.skydoves.powerspinner.IconSpinnerAdapter;
import com.skydoves.powerspinner.IconSpinnerItem;
import com.skydoves.powerspinner.PowerSpinnerView;
@@ -57,9 +59,17 @@ public class settingsFragment extends Fragment {
IconSpinnerAdapter iconSpinnerAdapter = new IconSpinnerAdapter(spinnerView);
spinnerView.setSpinnerAdapter(iconSpinnerAdapter);
spinnerView.setItems(iconSpinnerItems);
spinnerView.selectItemByIndex(0);
spinnerView.setLifecycleOwner(this);
if(!iconSpinnerItems.isEmpty()){
spinnerView.selectItemByIndex(0);
}
alert("test", v0.writeLine(1, "oeseo"));
alert("test", v0.readLine(0));
return root;
}
File diff suppressed because one or more lines are too long
@@ -40,7 +40,7 @@ public class generatorView extends ConstraintLayout {
private final int minQrSpeed = 300 + maxQrSpeed - 1;
private int minQrSize = 0;
private final int maxQrSize = 500;
private final int maxQrSize = 800;
private int qrSize = 200;
private final int defaultQrDelay = 419;
@@ -71,16 +71,17 @@ public class generatorView extends ConstraintLayout {
Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
// The Charset must be UTF-8, Or data will not be transferred properly. IDK why.
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "ISO-8859-1");
// hints.put(EncodeHintType.);
hints.put(EncodeHintType.MARGIN, 0); /* default = 4 */
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result;
try {
result = writer.encode(contents, BarcodeFormat.QR_CODE, size, size, hints);
} catch (IllegalArgumentException iae) {
result = writer.encode(contents, BarcodeFormat.DATA_MATRIX, size, size, hints);
} catch (IllegalArgumentException e) {
// Unsupported format
e.printStackTrace();
return null;
}
@@ -195,7 +196,7 @@ public class generatorView extends ConstraintLayout {
try {
// alert("test", ""+Math.ceil((double)data.length()/(double)qrSize));
qrBitmaps.add(generateQrCode(
fileEditor.byteToChar(fileEditor.internalDataVersion) +
fileEditor.byteToChar(fileEditor.internalDataVersion) +
String.valueOf(fileEditor.byteToChar(randID)) +
fileEditor.byteToChar(i) +
fileEditor.byteToChar(qrCount - 1) +
@@ -15,7 +15,6 @@ import android.widget.SeekBar;
import androidx.annotation.NonNull;
import androidx.annotation.OptIn;
import androidx.camera.core.AspectRatio;
import androidx.camera.core.Camera;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.ExperimentalGetImage;
import androidx.camera.core.ImageAnalysis;
@@ -28,7 +27,7 @@ import androidx.lifecycle.LifecycleOwner;
import com.astatin3.scoutingapp2025.databinding.FragmentTransferBinding;
import com.astatin3.scoutingapp2025.fileEditor;
import com.astatin3.scoutingapp2025.qrScanTask;
import com.astatin3.scoutingapp2025.codeScanTask;
import com.google.common.util.concurrent.ListenableFuture;
import java.nio.ByteBuffer;
@@ -111,7 +110,7 @@ public class scannerView extends ConstraintLayout {
}
public void scanQRCode(Bitmap bitmap) {
qrScanTask async = new qrScanTask();
codeScanTask async = new codeScanTask();
async.setImage(bitmap);
async.onResult(data -> {
if(data != null){