Open DavidAntonin opened 4 months ago
Yes - I am doing just that: void displayJPEG(const String& comment) { if (!FlashFS.begin()) { Serial.println("An error occurred while mounting FlashFS"); return; }
if (FlashFS.exists("/temp.jpg")) { Serial.println("Removing existing temporary file"); FlashFS.remove("/temp.jpg"); } // Render the image gfx->fillScreen(BLACK); // Clear the screen delay(10); digitalWrite(TFT_BL, LOW); delay(10); bool downloaded = getFile(comment, "/temp.jpg", FlashFS);
if (downloaded) { uint16_t imageWidth, imageHeight;
TJpgDec.setCallback(tftOutput);
TJpgDec.setSwapBytes(false);
// Get the dimensions of the image
TJpgDec.getFsJpgSize(&imageWidth, &imageHeight, "/temp.jpg", FlashFS);
// Check if the image fits within the screen size
if (imageWidth <= gfx->width() && imageHeight <= gfx->height()) {
// If the image fits, calculate starting x and y coordinates to center the image
int16_t startX = (gfx->width() - imageWidth) / 2;
int16_t startY = (gfx->height() - imageHeight) / 2;
digitalWrite(TFT_BL, HIGH);
if (TJpgDec.drawFsJpg(startX, startY, "/temp.jpg", FlashFS)) {
Serial.println("JPEG image displayed");
} else {
Serial.println("Failed to display the JPEG image");
}
} else {
// If the image does not fit, scale it
int scaleFactor = min((float)gfx->width() / imageWidth, (float)gfx->height() / imageHeight);
TJpgDec.setJpgScale(scaleFactor);
// Calculate the scaled dimensions
uint16_t scaledWidth = imageWidth * scaleFactor;
uint16_t scaledHeight = imageHeight * scaleFactor;
// Calculate starting x and y coordinates to center the image
int16_t startX = (gfx->width() - scaledWidth) / 2;
int16_t startY = (gfx->height() - scaledHeight) / 2;
if (TJpgDec.drawFsJpg(startX, startY, "/temp.jpg", FlashFS)) {
Serial.println("JPEG image displayed");
} else {
Serial.println("Failed to display the JPEG image");
}
}
if (FlashFS.remove("/temp.jpg")) {
Serial.println("Temporary file removed");
} else {
Serial.println("Failed to remove temporary file");
}
} else { Serial.println("Failed to download image"); }
FlashFS.end(); }
Getting the file:
bool getFile(const String& url, const char* fileName, fs::FS& fs = FlashFS) { Serial.print("[HTTP] begin...\n"); http.begin(url); Serial.print("[HTTP] GET...\n"); int httpCode = http.GET();
if (httpCode > 0) { Serial.printf("[HTTP] GET... code: %d\n", httpCode); if (httpCode == HTTP_CODE_OK) { int fileSize = http.getSize(); int maxFileSize = 200 * 1024; // 200KB - include in options if (fileSize <= maxFileSize) { File f = fs.open(fileName, FILE_WRITE); if (f) { int bytesWritten = http.writeToStream(&f); f.close(); Serial.printf("[HTTP] Bytes downloaded and saved: %d\n", bytesWritten); http.end(); return true; } } else { Serial.println("File size is larger than 100KB. Skipping download."); gfx->fillScreen(WHITE); gfx->setTextColor(BLACK); gfx->setCursor(30, 30); gfx->setTextSize(2); gfx->println("Error: File size is too big"); delay(2000); // Clear the files from FlashFS (if necessary) File root = FlashFS.open("/"); if (root) { File file = root.openNextFile(); while (file) { Serial.print("Removing file"); Serial.println(file.name()); FlashFS.remove(file.name()); file = root.openNextFile(); } } } } } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); }
http.end(); return false; }
Hello, it is possible to load JPG image from url and display it on TFT? Thanks David