/** * Saves a PNG Image * * @param resourceName is the name of the PNG image to save. * @param image the Image to save. */ public static void savePngImage(String RMSName, String resourceName, Image image) throws Exception { RecordStore imagesRS = null; int height, width; if (resourceName == null) return; // resource name is required // Calculate needed size and allocate buffer area height = image.getHeight(); width = image.getWidth(); int[] imgRgbData = new int[width * height]; try { image.getRGB(imgRgbData, 0, width, 0, 0, width, height); imagesRS = RecordStore.openRecordStore(RMSName, true); // ByteArrayOutputStream bout = new ByteArrayOutputStream(); DataOutputStream dout = new DataOutputStream(bout); // Serialize the image name dout.writeUTF(resourceName); dout.writeInt(width); dout.writeInt(height); dout.writeLong(System.currentTimeMillis()); dout.writeInt(imgRgbData.length); // Serialize the image raw data for (int i = 0; i < imgRgbData.length; i++) { dout.writeInt(imgRgbData[i]); } dout.flush(); dout.close(); byte[] data = bout.toByteArray(); imagesRS.addRecord(data, 0, data.length); } catch (RecordStoreFullException e) { Log.error("The Record Store is full, can't save any more tiles"); throw new RecordStoreFullException("Meh"); } catch (Exception e) { e.printStackTrace(); } finally { try { // Close the Record Store if (imagesRS != null) imagesRS.closeRecordStore(); } catch (Exception e) { Log.error("Exception in finally clause while saving png image " + e.getMessage()); } } }
/** * Return a list of all the images stored in this RMS * * @param recordStore */ public static Vector getImageList(String recordStore) { Log.debug("Getting Image list store name=[" + recordStore + "]"); RecordStore imagesRS = null; Vector v = new Vector(); try { imagesRS = RecordStore.openRecordStore(recordStore, true); RecordEnumeration re = imagesRS.enumerateRecords(null, null, true); if (re != null) { Log.debug("ImageRMSUtils: Record Enumeration was not null "); int numRecs = re.numRecords(); // For each record for (int i = 0; i < numRecs; i++) { // Get the next record's ID int recId = re.nextRecordId(); // throws // InvalidRecordIDException // Get the record Log.debug("Got a record"); byte[] rec = imagesRS.getRecord(recId); ByteArrayInputStream bin = new ByteArrayInputStream(rec); DataInputStream din = new DataInputStream(bin); v.addElement(din.readUTF()); bin.close(); } } else { Log.error("ImageRMSUtils Exception Record Enumeration was null "); } Log.debug("Retrieved " + v.size() + " images from RMS"); } catch (InvalidRecordIDException ignore) { Log.error("RecordId Exception " + ignore.getMessage()); } catch (Exception e) { Log.error("ImageRMSUtils Exception " + e.getMessage()); } finally { try { // Close the Record Store if (imagesRS != null) imagesRS.closeRecordStore(); } catch (Exception ignore) { // Ignore } } return v; }
/** * Load image with specified name * * @param recordStore is the name of the record store. * @param resourceName is the name of the image to load * @return the loaded Image or null. */ public static Image loadPngFromRMS(String recordStore, String resourceName) { RecordStore imagesRS = null; Image img = null; try { imagesRS = RecordStore.openRecordStore(recordStore, true); RecordEnumeration re = imagesRS.enumerateRecords(null, null, true); int numRecs = re.numRecords(); // For each record for (int i = 0; i < numRecs; i++) { // Get the next record's ID int recId = re.nextRecordId(); // throws // InvalidRecordIDException // Get the record byte[] rec = imagesRS.getRecord(recId); // ByteArrayInputStream bin = new ByteArrayInputStream(rec); DataInputStream din = new DataInputStream(bin); String name = din.readUTF(); // If this is the image we are looking for, load it. if (name.equals(resourceName) == false) continue; int width = din.readInt(); int height = din.readInt(); long timestamp = din.readLong(); int length = din.readInt(); int[] rawImg = new int[width * height]; // Serialize the image raw data for (i = 0; i < length; i++) { rawImg[i] = din.readInt(); } img = Image.createRGBImage(rawImg, width, height, false); din.close(); bin.close(); } } catch (InvalidRecordIDException ignore) { // End of enumeration, ignore } catch (Exception e) { Log.error("Exception while retrieving Image: " + e.getMessage()); } finally { try { // Close the Record Store if (imagesRS != null) imagesRS.closeRecordStore(); } catch (Exception ignore) { // Ignore } } return img; }