📄 utility.java
字号:
package com.ismyway.util;
import java.util.Calendar;
import javax.microedition.lcdui.Image;
import com.ismyway.anyview.others.Configure;
public class Utility {
public final static int INNER = 0; // 文件路径,表示文件在JAR文件中
public final static int EXTEND = 1; // 文件路径,表示文件在外部在座空间中
public final static byte PNG = 1;
public final static byte JPEG = 2;
public final static byte GIF = 3;
public final static int byte2int(byte[] b) {
int value = b[3] & 0xff | (b[2] & 0xff) << 8 | (b[1] & 0xff) << 16 | (b[0] & 0xff) << 24;
return value;
}
public final static int byte2byte(byte[] b) {
int value = b[0] & 0xff;
return value;
}
public final static String currentDate() {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DATE);
StringBuffer sb = new StringBuffer();
sb.append(year).append("-").append(month).append("-").append(day);
return sb.toString();
}
public final static String currentTime() {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR);
int min = calendar.get(Calendar.MINUTE);
int sec = calendar.get(Calendar.SECOND);
int pm = calendar.get(Calendar.AM_PM);
hour = pm == 1 ? hour + 12 : hour;
StringBuffer sb = new StringBuffer();
sb.append(hour < 10 ? "0" : "").append(hour).append(":");
sb.append(min < 10 ? "0" : "").append(min).append(":");
sb.append(sec < 10 ? "0" : "").append(sec);
return sb.toString();
}
public final static Image doubleImg(Image img) {
if (null == img) {
return null;
}
int w = img.getWidth();
int h = img.getHeight();
int[] r = new int[w * h];
img.getRGB(r, 0, w, 0, 0, w, h);
int[] d = new int[w * h * 4];
int p = 0;
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
int o = row * w * 4 + col * 2;
d[o] = r[p];
d[o + 1] = r[p];
o += w * 2;
d[o] = r[p];
d[o + 1] = r[p];
p++;
}
}
Image image = Image.createRGBImage(d, w * 2, h * 2, true);
r = d = null;
Configure.gc();
return image;
}
public final static Image doubleImgAnti(Image img) {
if (null == img) {
return null;
}
int w = img.getWidth();
int h = img.getHeight();
int[] r = new int[w * h];
img.getRGB(r, 0, w, 0, 0, w, h);
int nw = 2 * w - 1;
int nh = 2 * h - 1;
int[] d = new int[nw * nh];
for (int i = 0; i < d.length; i++) {
d[i] = 0xffff0000;
}
int p = 0;
//将点抽开
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
int o = 2 * row * nw + 2 * col;
d[o] = r[p];
p++;
}
}
//横向羽化
p = nw;
for (int row = 0; row < h - 1; row++) {
for (int col = 0; col < w; col++) {
int o = (row * 2 + 1) * nw + 2 * col;
d[o] = avgColor(d[o - nw], d[o + nw]);
}
}
//纵向羽化
for (int row = 0; row < h; row++) {
for (int col = 0; col < w - 1; col++) {
int o = row * 2 * nw + 2 * col + 1;
d[o] = avgColor(d[o - 1], d[o + 1]);
}
}
//交叉点羽化
for (int row = 0; row < h - 1; row++) {
for (int col = 0; col < w - 1; col++) {
int o = (row * 2 + 1) * nw + 2 * col + 1;
d[o] = avgColor(d[o - nw], d[o + nw], d[o - 1], d[o + 1]);
}
}
Image image = Image.createRGBImage(d, nw, nh, true);
r = d = null;
Configure.gc();
return image;
}
public static int avgColor(int c1, int c2) {
int a1 = (c1 >> 24) & 0xff;
int r1 = (c1 >> 16) & 0xff;
int g1 = (c1 >> 8) & 0xff;
int b1 = c1 & 0xff;
int a2 = (c2 >> 24) & 0xff;
int r2 = (c2 >> 16) & 0xff;
int g2 = (c2 >> 8) & 0xff;
int b2 = c2 & 0xff;
a1 = ((a1 + a2) >> 1) & 0xff;
r1 = ((r1 + r2) >> 1) & 0xff;
g1 = ((g1 + g2) >> 1) & 0xff;
b1 = ((b1 + b2) >> 1) & 0xff;
int c = a1 << 24 | r1 << 16 | g1 << 8 | b1;
return c;
}
public static int avgColor(int c1, int c2, int c3, int c4) {
int a1 = (c1 >> 24) & 0xff;
int r1 = (c1 >> 16) & 0xff;
int g1 = (c1 >> 8) & 0xff;
int b1 = c1 & 0xff;
int a2 = (c2 >> 24) & 0xff;
int r2 = (c2 >> 16) & 0xff;
int g2 = (c2 >> 8) & 0xff;
int b2 = c2 & 0xff;
int a3 = (c3 >> 24) & 0xff;
int r3 = (c3 >> 16) & 0xff;
int g3 = (c3 >> 8) & 0xff;
int b3 = c3 & 0xff;
int a4 = (c4 >> 24) & 0xff;
int r4 = (c4 >> 16) & 0xff;
int g4 = (c4 >> 8) & 0xff;
int b4 = c4 & 0xff;
a1 = ((a1 + a2 + a3 + a4) >> 2) & 0xff;
r1 = ((r1 + r2 + r3 + r4) >> 2) & 0xff;
g1 = ((g1 + g2 + g3 + g4) >> 2) & 0xff;
b1 = ((b1 + b2 + b3 + b4) >> 2) & 0xff;
int c = a1 << 24 | r1 << 16 | g1 << 8 | b1;
return c;
}
/**
* 取得给定文件名的后缀
* 如果是文件夹,返回null
* @param filename
* @return
*/
public final static String getSuffix(String filename) {
String suffix = null;
if (null == filename || filename.endsWith("/")) {
return null;
}
int off = filename.lastIndexOf('.');
if (off > 0) {
suffix = filename.substring(off).toLowerCase();
}
return suffix;
}
public static Image getScaledImage(Image dispImage, int width, int height) {
return dispImage;
}
/**
* 将图片按red, green, blue的增量变换图片
* @param img
* @param red
* @param green
* @param blue
* @param alpha
* @return
*/
public final static Image transImage(Image img, int red, int green, int blue, boolean alpha) {
if (null == img) {
return null;
}
int w = img.getWidth();
int h = img.getHeight();
int[] array = new int[w * h];
img.getRGB(array, 0, w, 0, 0, w, h);
for (int i = 0; i < array.length; i++) {
if (array[i] != 0) {
int a = array[i] >> 24;
int r = (array[i] >> 16) & 0xff;
int g = (array[i] >> 8) & 0xff;
int b = array[i] & 0xff;
r += red;
g += green;
b += blue;
r = r > 0xff ? 0xff : r;
r = r < 0 ? 0 : r;
g = g > 0xff ? 0xff : g;
g = g < 0 ? 0 : g;
b = b > 0xff ? 0xff : b;
b = b < 0 ? 0 : b;
array[i] = (a << 24) | (r << 16) | (g << 8) | b;
}
}
Image image = Image.createRGBImage(array, w, h, alpha);
array = null;
Configure.gc();
return image;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -