📄 imageutil.java
字号:
package com.j2medev.ch8.mmapi;
import javax.microedition.lcdui.*;
public class ImageUtil {
//创建缩略图
public static Image createThumbnail(Image image,int width) {
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
int thumbWidth = width;
int thumbHeight = -1;
if (thumbHeight == -1)
thumbHeight = thumbWidth * sourceHeight / sourceWidth;
Image thumb = Image.createImage(thumbWidth, thumbHeight);
Graphics g = thumb.getGraphics();
for (int y = 0; y < thumbHeight; y++) {
for (int x = 0; x < thumbWidth; x++) {
g.setClip(x, y, 1, 1);
int dx = x * sourceWidth / thumbWidth;
int dy = y * sourceHeight / thumbHeight;
g.drawImage(image, x - dx, y - dy, Graphics.LEFT | Graphics.TOP);
}
}
Image immutableThumb = Image.createImage(thumb);
return immutableThumb;
}
//从int[]数组转换为byte[]
private byte[] int2byte(int[] data) {
byte[] buffer = new byte[4*data.length];
for(int i=0; i<data.length; i++) {
int p = data[i];
buffer[i*4+0] = (byte)((p & 0xff000000)>>>24);
buffer[i*4+1] = (byte)((p & 0xff0000)>>>16);
buffer[i*4+2] = (byte)((p & 0xff00)>>>8);
buffer[i*4+3] = (byte)((p & 0xff));
}
return buffer;
}
//从byte[]转换为int[]
private int[] byte2int(byte[] data) {
int[] buffer = new int[data.length/4];
for(int i=0; i<data.length/4; i++) {
int a = 0xff & data[i*4+0];
int r = 0xff & data[i*4+1];
int g = 0xff & data[i*4+2];
int b = 0xff & data[i*4+3];
buffer[i] = (a << 24) | (r << 16) | (g << 8) | b;
}
return buffer;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -