package com.zeph.j2me.rsm.image; import java.io.DataInputStream; import java.io.DataOutputStream; import javax.microedition.io.Connector; import javax.microedition.io.HttpConnection; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.Image; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; import javax.microedition.rms.RecordStore; import javax.microedition.rms.RecordStoreException; import javax.microedition.rms.RecordStoreFullException; import javax.microedition.rms.RecordStoreNotFoundException; public class ImageStoreMIDlet extends MIDlet implements CommandListener { private Display display; private Form form; private Command command; public ImageStoreMIDlet() { display = Display.getDisplay(this); form = new Form("下载图片,并存储,然后读取,再现实"); command = new Command("显示", Command.OK, 0); form.addCommand(command); form.setCommandListener(this); } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } protected void pauseApp() { } protected void startApp() throws MIDletStateChangeException { loadImage("http://localhost:8080/tomcat.gif"); display.setCurrent(form); } public void loadImage(String URL) { Image image = getImageFromURL(URL); if (image != null) { int imageWidth = image.getWidth(); int imageHeight = image.getHeight(); int[] imageData = new int[imageWidth * imageHeight]; image.getRGB(imageData, 0, imageWidth, 0, 0, imageWidth, imageHeight); byte[] data = new byte[imageWidth * imageHeight * 4]; convertIntArrayToByteArray(imageData, data); storeImage(imageWidth, imageHeight, data); } } public void convertIntArrayToByteArray(int[] imageData, byte[] data) { int dataLength = imageData.length; for (int i = 0; i < dataLength; i++) { int k = imageData[i]; for (int j = 0; j < 4; j++) { byte b = (byte) (k >> j * 8 & 0x00FF); data[i * 4 + j] = b; } } } public void storeImage(int imageWidth, int imageHeight, byte[] data) { try { RecordStore.deleteRecordStore("StoreImage"); } catch (RecordStoreNotFoundException e) { e.printStackTrace(); } catch (RecordStoreException e) { e.printStackTrace(); } try { RecordStore rs = RecordStore.openRecordStore("StoreImage", true); if (rs != null) { byte[] b = null; b = String.valueOf(imageWidth).getBytes(); rs.addRecord(b, 0, b.length); b = String.valueOf(imageHeight).getBytes(); rs.addRecord(b, 0, b.length); rs.addRecord(data, 0, data.length); } rs.closeRecordStore(); } catch (RecordStoreFullException e) { e.printStackTrace(); } catch (RecordStoreNotFoundException e) { e.printStackTrace(); } catch (RecordStoreException e) { e.printStackTrace(); } } public void readImage() { byte[] data = null; int imageWidth = 0; int imageHeight = 0; try { RecordStore rs = RecordStore.openRecordStore("StoreImage", true); if (rs != null) { int recordNum = rs.getNumRecords(); if (recordNum < 3) return; byte[] b = null; String s = null; b = rs.getRecord(1); s = new String(b); imageWidth = Integer.valueOf(s).intValue(); b = rs.getRecord(2); s = new String(b); imageHeight = Integer.valueOf(s).intValue(); data = rs.getRecord(3); } rs.closeRecordStore(); } catch (RecordStoreFullException e) { e.printStackTrace(); } catch (RecordStoreNotFoundException e) { e.printStackTrace(); } catch (RecordStoreException e) { e.printStackTrace(); } if (data != null) { int[] imageData = new int[(int) (data.length / 4)]; convertByteArrayToIntArray(data, imageData); displayImage(imageData, imageWidth, imageHeight); } } public void convertByteArrayToIntArray(byte[] data, int[] imageData) { int dataLength = data.length; for (int i = 0; i < dataLength; i = i + 4) { int j = (data[i] & 0x000000FF) | (data[i + 1] << 8 & 0x0000FF00) | (data[i + 2] << 16 & 0x00FF0000) | (data[i + 3] << 24 & 0xFF000000); imageData[i / 4] = j; } } public void displayImage(int[] imageData, int imageWidth, int imageHeight) { Image image = null; image = Image.createRGBImage(imageData, imageWidth, imageHeight, false); form.append(image); } public Image getImageFromURL(String url) { byte[] result = null; HttpConnection hcon; DataOutputStream os; DataInputStream is = null; int length = 0; try { hcon = (HttpConnection) Connector.open(url, Connector.READ_WRITE); hcon.setRequestMethod(HttpConnection.POST); hcon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); os = hcon.openDataOutputStream(); int rc = hcon.getResponseCode(); if (rc == HttpConnection.HTTP_OK) { is = new DataInputStream(hcon.openDataInputStream()); length = (int) hcon.getLength(); byte[] b = new byte[length]; int index = 0, hasRead = 0; while (hasRead != -1 && index < length) { hasRead = is.read(b, index, length - index); index += hasRead; } result = b; } is.close(); os.close(); hcon.close(); } catch (Exception ex) { return null; } finally { } return Image.createImage(result, 0, length); } public void commandAction(Command c, Displayable d) { if (c == command) { readImage(); } } }