Image processing J2ME黑白图像捕获
我想用j2me捕捉黑白图像 我可以拍摄彩色图像,但想拍摄黑白和灰度图像Image processing J2ME黑白图像捕获,image-processing,java-me,midlet,Image Processing,Java Me,Midlet,我想用j2me捕捉黑白图像 我可以拍摄彩色图像,但想拍摄黑白和灰度图像 在摄影机画布中的何处设置此属性 您很可能需要使用管理器的图像编码设置 通过使用以下代码,您可以将图像转换为灰度 import java.io.IOException; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.Image; import javax
在摄影机画布中的何处设置此属性 您很可能需要使用管理器的图像编码设置
通过使用以下代码,您可以将图像转换为灰度
import java.io.IOException;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
public class Grey extends MIDlet {
private Form frm;
public void startApp() {
if (frm == null) {
frm = new Form("Grey");
try {
Image img = makeGreyScale(Image.createImage("/test.png"));
frm.append(img);
} catch (IOException e) {
frm.append(e.toString());
}
Display.getDisplay(this).setCurrent(frm);
}
}
public void pauseApp() {
// do nothing
}
public void destroyApp(boolean b) {
// do nothing
}
// here is where the action is...
private static Image makeGreyScale(Image img) {
// work out how many pixels, and create array
int width = img.getWidth();
int height = img.getHeight();
int[] pixels = new int[width * height];
// get the pixel data
img.getRGB(pixels, 0, width, 0, 0, width, height);
// convert to grey scale
for (int i = 0; i < pixels.length; i++) {
// get one pixel
int argb = pixels[i];
// separate colour components
int alpha = (argb >> 24) & 0xff;
int red = (argb >> 16) & 0xff;
int green = (argb >> 8) & 0xff;
int blue = argb & 0xff;
// construct grey value
int grey = (((red * 30) / 100) + ((green * 59) / 100) + ((blue * 11) / 100)) & 0xff;
// reconstruct pixel with grey value - keep original alpha
argb = (alpha << 24) | (grey << 16) | (grey << 8) | grey;
// put back in the pixel array
pixels[i] = argb;
}
// create and return a new Image
return Image.createRGBImage(pixels, width, height, true);
}
import java.io.IOException;
导入javax.microedition.lcdui.Display;
导入javax.microedition.lcdui.Form;
导入javax.microedition.lcdui.Image;
导入javax.microedition.midlet.midlet;
公共级灰色延伸MIDlet{
私有形式frm;
公开作废startApp(){
如果(frm==null){
frm=新形式(“灰色”);
试一试{
Image img=makeGreyScale(Image.createImage(“/test.png”);
frm.追加(img);
}捕获(IOE异常){
frm.append(例如toString());
}
Display.getDisplay(this).setCurrent(frm);
}
}
公共无效pauseApp(){
//无所事事
}
公共应用程序(布尔b){
//无所事事
}
//这里是行动的地方。。。
私有静态图像生成灰度(图像img){
//计算出有多少像素,然后创建阵列
int width=img.getWidth();
int height=img.getHeight();
int[]像素=新int[宽度*高度];
//获取像素数据
getRGB(像素,0,宽度,0,0,宽度,高度);
//转换为灰度
对于(int i=0;i>24)和0xff;
int red=(argb>>16)和0xff;
绿色整数=(argb>>8)&0xff;
蓝色整数=argb&0xff;
//构造灰色值
整数灰色=((红色*30)/100)+((绿色*59)/100)+((蓝色*11)/100))&0xff;
//用灰度值重建像素-保留原始alpha
argb=(α