Fantastic Databases: the Downfall

After managing to do all the functions in separate code, I decided it was time to smash it all together into one working code. Which went surprisingly well.


import java.util.Base64;
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import de.bezier.data.sql.*;
import java.util.Date;
String[] filenames = new String [100];
String[] control = new String [100];
String path = "C:/test/";
String [] arrayForSave;
MySQL db;
int numeric = 0;
String b64string;
String user = "root";
String pass = "";
String database = "processing";

void setup() {

size(1200, 600);

db = new MySQL(this, "localhost", database, user, pass);
}

int checkdir() {
println("Listing all filenames in a directory: ");
String[] filenames = listFileNames(path);
printArray(filenames);
delay(5000);

String [] control = listFileNames(path);
printArray(control);
print("difference in length: ");
println(control.length - filenames.length);
return control.length - filenames.length;

}

void encode() {

String encoded = "";
String fileLocation = "C:/test/capture.png";
int numberofnewfiles = checkdir();
print("numberofnewfiles: ");
println(numberofnewfiles);

try {
encoded = encodeToBase64(fileLocation);
}
catch (IOException e) {
e.printStackTrace();
}

String [] arrayForSave = {encoded};
b64string = join(arrayForSave, "");

}
void sendData() {
if ( db.connect() ) {
if(b64string != null){
println("success");
} else {
println("failure");
}
//println( "INSERT INTO test (b64) VALUES ('" + b64string + "')" );
db.query("INSERT INTO test (b64) VALUES ('%s')", b64string);
numeric++;
}
}
void draw () {

if (numeric == 0) {

checkdir();
encode();
sendData();
}
}
String[] listFileNames(String dir) {
File file = new File(dir);
if (file.isDirectory()) {
String names[] = file.list();
return names;
} else {
// If it's not a directory
return null;
}
}

private String encodeToBase64(String fileLoc) throws IOException, FileNotFoundException {

File originalFile = new File(fileLoc);
String encodedBase64 = null;

FileInputStream fileInputStreamReader = new FileInputStream(originalFile);
byte[] bytes = new byte[(int)originalFile.length()];
fileInputStreamReader.read(bytes);
encodedBase64 = new String(Base64.getEncoder().encode(bytes));
fileInputStreamReader.close();

return encodedBase64;
}

The following days I spent with a lot of teachers going over and optimizing my code. I also realized that it was easier moving the files from the

DSLR output location, than making the code understand about duplicates. I had quite some trouble with moving the files, so I randomly one night decided to make an stackoverflow post, that would solve my problems.

https://stackoverflow.com/questions/56819637/moving-a-file-in-processing

I woke up to a working solution and managed to implement it.


import java.util.Base64;
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import de.bezier.data.sql.*;
import java.util.Date;
import java.nio.file.Files;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
//String[] filenames = new String [100];
String[] control = new String [100];
String path = "C:/test/";
String newpath = "C:/test123/";
String [] arrayForSave;
MySQL db;
int numeric = 0;
String b64string;
String user = "root";
String pass = "";
String database = "processing";

int nr_of_files = 0;
String[] filenames;
void setup() {

size(1200, 600);

db = new MySQL(this, "localhost", database, user, pass);
checkdir();
}

void draw () {

if (numeric == 0) {

if ( checkdir() > 0 ) {
while ( nr_of_files  0) {

try {
encoded = encodeToBase64(fileLocation);
}
catch (IOException e) {
e.printStackTrace();
}

String [] arrayForSave = {encoded};
b64string = join(arrayForSave, "");
picnames++;
println(picnames);

}
}
void sendData() {
if ( db.connect() ) {
if (b64string != null) {
db.query("INSERT INTO test (b64) VALUES ('%s')", b64string);
// numeric++;
println("success");
// Files.move(Paths.get("C:/test/) );
} else {
println("failure");
}
//println( "INSERT INTO test (b64) VALUES ('" + b64string + "')" );
}
}
String[] listFileNames(String dir) {
File file = new File(dir);
if (file.isDirectory()) {
String names[] = file.list();
return names;
} else {
// If it's not a directory
return null;
}
}

private String encodeToBase64(String fileLoc) throws IOException, FileNotFoundException {

File originalFile = new File(fileLoc);
String encodedBase64 = null;

FileInputStream fileInputStreamReader = new FileInputStream(originalFile);
byte[] bytes = new byte[(int)originalFile.length()];
fileInputStreamReader.read(bytes);
encodedBase64 = new String(Base64.getEncoder().encode(bytes));
fileInputStreamReader.close();

return encodedBase64;
}

In the end, I encountered a lot of small problems along the way. Namely, that the database would overload after 5 images for some peculiar reason. I decided to search around for alternative methods and I found out that base64 is quite a bad practice. Usually the images are stored on a server, which means I would have to get the location of the image on the server. Furthermore, I would also have to upload the image to a server and i’m not sure how to do that. I decided to cut out base64 and work with the image file paths.

Leave a comment

Blog at WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started