img2img-turbo / DISPLAYER /FullScreenImageLoader.as
Inmental's picture
Upload folder using huggingface_hub
343e5a8 verified
 package {
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.utils.*;
import flash.text.*;
import com.adobe.images.PNGEncoder;
import com.adobe.crypto.MD5;
import com.sociodox.utils.Base64;
import flash.filesystem.File;
import flash.filesystem.FileStream;
import flash.filesystem.FileMode;
public class FullScreenImageLoader extends Sprite {
private var apiUrl:String;
private var imageWidth:int;
private var imageHeight:int;
private var imageX:int;
private var imageY:int;
private var refreshInterval:int = 2000; // Refresh every 2 seconds
private var timer:Timer;
private var bitmap:Bitmap;
private var currentChecksum:String = ""; // Store the checksum of the currently displayed image
private var errorMessage:TextField;
public function FullScreenImageLoader() {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.displayState = StageDisplayState.FULL_SCREEN;
// Load configuration from JSON file
loadConfig();
bitmap = new Bitmap();
addChild(bitmap);
// Set up the timer to refresh the image
timer = new Timer(refreshInterval);
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();
// Create an error message text field for display
errorMessage = new TextField();
errorMessage.defaultTextFormat = new TextFormat("Arial", 20, 0xFF0000);
errorMessage.width = stage.stageWidth;
errorMessage.height = 50;
errorMessage.y = stage.stageHeight - 50;
errorMessage.selectable = false;
addChild(errorMessage);
// Initial load
loadImage();
}
private function loadConfig():void {
var configFile:File = File.applicationDirectory.resolvePath("config.json");
if (configFile.exists) {
var fileStream:FileStream = new FileStream();
fileStream.open(configFile, FileMode.READ);
var configData:String = fileStream.readUTFBytes(fileStream.bytesAvailable);
fileStream.close();
var config:Object = JSON.parse(configData);
apiUrl = config.api_url;
imageWidth = config.size.width;
imageHeight = config.size.height;
imageX = config.position.x;
imageY = config.position.y;
trace("Configuration loaded: ", apiUrl, imageWidth, imageHeight, imageX, imageY);
} else {
trace("Config file not found: config.json");
errorMessage.text = "Config file not found: config.json";
}
}
private function onTimer(event:TimerEvent):void {
loadImage();
}
private function loadImage():void {
var request:URLRequest = new URLRequest(apiUrl + "/get_status");
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, onImageLoadComplete);
loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
loader.load(request);
}
private function onImageLoadComplete(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
try {
var response:Object = JSON.parse(loader.data);
if (response.hasOwnProperty("checksum")) {
var newChecksum:String = response.checksum;
// Compare checksums, if different, update the image
if (newChecksum != currentChecksum) {
currentChecksum = newChecksum;
if (response.hasOwnProperty("image_base64") && response.image_base64 != "") {
displayNewImage(response.image_base64);
} else {
trace("No image data found.");
errorMessage.text = "No image data found.";
}
}
} else {
trace("Invalid response from server.");
errorMessage.text = "Invalid response from server.";
}
} catch (error:Error) {
trace("Error parsing response: " + error.message);
errorMessage.text = "Error parsing response: " + error.message;
}
}
private function displayNewImage(base64Image:String):void {
try {
var byteArray:ByteArray = Base64.decode(base64Image);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageDecoded);
loader.loadBytes(byteArray);
} catch (error:Error) {
trace("Error decoding image: " + error.message);
errorMessage.text = "Error decoding image: " + error.message;
}
}
private function onImageDecoded(event:Event):void {
var loader:Loader = Loader(event.target.loader);
var bitmapData:BitmapData = Bitmap(loader.content).bitmapData;
// Update the display
bitmap.bitmapData = bitmapData;
bitmap.width = imageWidth;
bitmap.height = imageHeight;
bitmap.x = imageX;
bitmap.y = imageY;
bitmap.smoothing = true;
}
private function onIOError(event:IOErrorEvent):void {
trace("Error loading image: " + event.text);
errorMessage.text = "Error loading image: " + event.text;
}
}
}