J2ME从服务器端下载图片,在RMS中存储,然后在读取显示(非原创代码,整合应用而已)

 

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();
		}
	}
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值