J2ME黑白图像采集

发布于 2024-10-03 16:13:42 字数 72 浏览 0 评论 0原文

我想使用 j2me 捕获黑白图像 我可以捕捉彩色图像,但想要捕捉黑白和灰度图像,

我在相机画布中的哪里设置此属性?

I want to capture black and white image using j2me
i am ok with color image capturing but wants to capture black and white and gray scale iamge

where do i set this property in camera canvas??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

—━☆沉默づ 2024-10-10 16:13:42

通过使用以下代码,您可以将图像转换为灰度

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

}

by using following code you can convert image to grey scale

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

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文