⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ncanvas.java

📁 基于j2me的任意角度图片旋转
💻 JAVA
字号:
package com.nokia;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.nokia.mid.ui.DirectGraphics;
import com.nokia.mid.ui.DirectUtils;
import com.nokia.mid.ui.FullCanvas;

/**
 * 
 * @author pandonix 基于4444格式的实现
 * 
 */
public class NCanvas extends FullCanvas {

	Image img;
	
	Image imgTran;

	short[] pixels;

	int width, height;
	
	int radius = 198;

	public NCanvas() {
		setFullScreenMode(true);
		try {
			img = Image.createImage("/2.png");
		} catch (IOException ie) {
			ie.printStackTrace();
		}
		width = img.getWidth();
		height = img.getHeight();
		pixels = pixelsProduce(img);
//		imgTran = createImg(pixels,width,height);
		pixels = Rotation_4444.getRotatePixels(pixels, width, height, radius, 30);
	}

	protected void paint(Graphics g) {
		// TODO Auto-generated method stub
		g.setColor(0);
		g.fillRect(0, 0, 176, 208);
//		g.drawImage(imgTran, 0, 10, 0);
//		g.drawImage(img,70,10,0);
		drawRotation(g, 0, 10);
	}
	
	/**
	 * 使用NokiaUI获取像素数组
	 * @param src
	 * @return
	 */
	public short[] pixelsProduce(Image src)
	{
		int w = src.getWidth();
		int h = src.getHeight();
		short[] _pixels = new short[w * h];
		//创建可变图像
		Image img = DirectUtils.createImage(w, h, 0);
		Graphics g = img.getGraphics();
		DirectGraphics dg = DirectUtils.getDirectGraphics(g);
		//先将原图绘制到创建好的可变图像上
		dg.drawImage(src,0,0,0,0);
		//再获取该可变图像的像素数组
		dg.getPixels(_pixels,0, w, 0, 0, w, h,4444);
		return _pixels;
	}
	
	public Image createImg(short[] pixels,int _width,int _height)
	{
		
		Image img = DirectUtils.createImage(_width, _height, 0);
		Graphics g = img.getGraphics();
		DirectGraphics dg = DirectUtils.getDirectGraphics(g);
		dg.drawPixels(pixels, true, 0, _width, 0, 0, _width, _height, 0,4444);
		return img;
	}
	
	/**
	 * 将8888格式转化为4444格式,其中还包括565格式的转换算法
	 * @param src
	 * @return
	 */
	private short[] convert8To4(Image src) {
		int w = src.getWidth();
		int h = src.getHeight();
		int[] _pixels = new int[w * h];
		src.getRGB(_pixels, 0, w, 0, 0, w, h);
		short[] results = new short[w * h];
		int idx = 0;
		int idy = 0;

		int format = 4444;
		for (int i = 0; i < h; i++) {
			if (format == 4444) {
				for (int j = 0; j < w; j++) {
					int p = _pixels[idx++] >> 4 & 0xf0f0f0f;
					p = (p >> 4 | p) & 0xff00ff;
					p = p >> 8 | p;
					p &= 0xffff;
					results[idy++] = (short) p;
				}
			} else if (format == 565) {
				for (int j = 0; j < w; j++) {
					int l3 = _pixels[idx++];
					l3 = l3 >> 3 & 0x1f | l3 >> 5 & 0x7e0 | l3 >> 8 & 0xf800;
					results[idy++] = (short) l3;
				}
			} else {
				throw new IllegalArgumentException();
			}
		}
		return results;
	}

	private void drawRotation(Graphics g, int x, int y) {
		 DirectGraphics dg = DirectUtils.getDirectGraphics(g);
		 dg.drawPixels(pixels, true, 0, radius, x, y, radius, radius, 0,4444);
	}

	/*
	 * 在这里实现了Nokia的特有API 大家可以扩展该方法 由BB提供
	 */
	private void drawPixels(Graphics g, short[] pixels, boolean transparency,
			int offset, int scanlength, int x, int y, int width, int height,
			int manipulation, int format) {
		int l1 = map2Manipulation(manipulation);
		int j1;
		int k1;
		if ((l1 & 4) != 0) {
			j1 = height;
			k1 = width;
		} else {
			j1 = width;
			k1 = height;
		}

		short newPixels[] = new short[j1 * k1];
		// 获取pixels数组
		if (manipulation == 0) {
			newPixels = pixels;
		} else
			for (int i2 = 0; i2 < k1; i2++) {
				for (int j2 = 0; j2 < j1; j2++) {
					int j = j2;
					int k = i2;
					if ((l1 & 1) != 0)
						j = j1 - 1 - j;
					if ((l1 & 2) != 0)
						k = k1 - 1 - k;
					if ((l1 & 4) != 0) {
						int k2 = j;
						j = k;
						k = k2;
					}
					newPixels[j1 * i2 + j2] = pixels[width * k + j];
				}
			}

		int off = offset;

		int vw = x + j1;

		int vh = y + k1;

		for (int idy = y; idy < vh; idy++) {

			int voff = off;

			for (int idx = x; idx < vw; idx++) {

				short pixel = newPixels[voff++];

				int k3 = idx;

				for (; idx < vw - 1 && newPixels[voff] == pixel; voff++)
					idx++;
				if ((pixel >> 12 & 0xff) != 0) {
					int l3 = 0xf0 & pixel << 4;
					l3 |= 0xf000 & pixel << 8;
					l3 |= 0xf00000 & pixel << 12;
					// if(l3 == 0xffffff){
					// System.out.println("bai");
					// }
					g.setColor(l3);
					g.drawLine(k3, idy, idx, idy);

				}

			}

			off += j1;

		}

	}

	private static int map2Manipulation(int i) throws IllegalArgumentException {

		int j = 0;

		if ((i & 0x2000) != 0)

			j ^= 1;

		if ((i & 0x4000) != 0)

			j ^= 2;

		switch (i & 0xffff9fff) {

		case 90: // 'Z'

			j ^= 6;

			break;

		case 180:

			j ^= 3;

			break;

		case 270:

			j ^= 5;

			break;

		default:

			throw new IllegalArgumentException();

		case 0: // '\0'

			break;

		}

		return j;

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -