如何在Android中逐像素显示位图?

发布于 2025-01-08 16:18:31 字数 1907 浏览 5 评论 0原文

我正在尝试逐像素显示位图图像(这明确意味着有一些延迟)。
为此,我使用两个“for循环”,但它只打印一行像素...

我的代码:

Button start = (Button) findViewById(R.id.start);
start.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //Toast.makeText(getBaseContext(), "Printing...", Toast.LENGTH_SHORT).show();
        iImageArray = new int[bMap.getWidth()* bMap.getHeight()];                                   //initializing the array for the image size
        bMap.getPixels(iImageArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());   //copy pixel data from the Bitmap into the 'intArray' array  
             
        //Canvas canvas = new Canvas (bMap);
        //replace the red pixels with yellow ones  
        for (int i=0; i < bMap.getHeight(); i++) {  
            for(int j=0; j<bMap.getWidth(); j++) {
                iImageArray[j] = 0xFFFFFFFF;
            } 
        } 
        bMap = Bitmap.createBitmap(iImageArray, bMap.getWidth(), bMap.getHeight(), Bitmap.Config.ARGB_8888);//Initialize the bitmap, with the replaced color
        image.setImageBitmap(bMap);
        //canvas.drawPoints(iImageArray, 0, bMap.getHeight()*bMap.getWidth(), paint);
    }
});

我想以灰度打印位图,为此我找到了这段代码...

public Bitmap toGrayscale(Bitmap bmpOriginal) {        
    int width, height;
    height = bmpOriginal.getHeight();
    width = bmpOriginal.getWidth();    

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);

    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);

    c.drawBitmap(bmpOriginal, 0, 0, paint);

    return bmpGrayscale;
}

它是 iImageArray[i] = 0xFFFFFFFF; 我必须测试,而不是实际的灰度值......

I am trying to display a bitmap image pixel by pixel (which explicitly means with some delay).
For that I am using two "for-loops", but it prints only a single row of pixels...

My code:

Button start = (Button) findViewById(R.id.start);
start.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //Toast.makeText(getBaseContext(), "Printing...", Toast.LENGTH_SHORT).show();
        iImageArray = new int[bMap.getWidth()* bMap.getHeight()];                                   //initializing the array for the image size
        bMap.getPixels(iImageArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());   //copy pixel data from the Bitmap into the 'intArray' array  
             
        //Canvas canvas = new Canvas (bMap);
        //replace the red pixels with yellow ones  
        for (int i=0; i < bMap.getHeight(); i++) {  
            for(int j=0; j<bMap.getWidth(); j++) {
                iImageArray[j] = 0xFFFFFFFF;
            } 
        } 
        bMap = Bitmap.createBitmap(iImageArray, bMap.getWidth(), bMap.getHeight(), Bitmap.Config.ARGB_8888);//Initialize the bitmap, with the replaced color
        image.setImageBitmap(bMap);
        //canvas.drawPoints(iImageArray, 0, bMap.getHeight()*bMap.getWidth(), paint);
    }
});

And I want to print the bitmap in grayscale and for that I found this code...

public Bitmap toGrayscale(Bitmap bmpOriginal) {        
    int width, height;
    height = bmpOriginal.getHeight();
    width = bmpOriginal.getWidth();    

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);

    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);

    c.drawBitmap(bmpOriginal, 0, 0, paint);

    return bmpGrayscale;
}

It is iImageArray[i] = 0xFFFFFFFF; that I have to test, not the actual grayscale value...

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

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

发布评论

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

评论(2

美胚控场 2025-01-15 16:18:31

除了内部循环之外,您的代码看起来基本上是正确的。您的评论说您将它们设置为黄色,但实际上您正在存储白色。该循环只会影响第一行像素,因为数组索引的范围为 0 到 width-1。您可以通过乘以 (i*width+j) 来计算索引,也可以保留一个递增的计数器。

原始版本:

//replace the red pixels with yellow ones  
for (int i=0; i < bMap.getHeight(); i++)  
{  
    for(int j=0; j<bMap.getWidth(); j++)
    {
        iImageArray[j] = 0xFFFFFFFF;
    } 
} 

修复版本:

//replace the red pixels with yellow ones
int iWidth = bMap.getWidth();  
for (int i=0; i < bMap.getHeight(); i++)  
{  
    for(int j=0; j<bMap.getWidth(); j++)
    {
        iImageArray[(i*iWidth)+j] = 0xFF00FFFF; // actual value of yellow
    } 
} 

Your code looks basically correct except for your inner loop. Your comment says that you're setting them to yellow, but you're actually storing white. The loop will only affect the first row of pixels because your array index has a range of 0 to width-1. You can either calculate the index by multiplying (i*width+j) or keep a counter that increments.

Original Version:

//replace the red pixels with yellow ones  
for (int i=0; i < bMap.getHeight(); i++)  
{  
    for(int j=0; j<bMap.getWidth(); j++)
    {
        iImageArray[j] = 0xFFFFFFFF;
    } 
} 

Fixed Version:

//replace the red pixels with yellow ones
int iWidth = bMap.getWidth();  
for (int i=0; i < bMap.getHeight(); i++)  
{  
    for(int j=0; j<bMap.getWidth(); j++)
    {
        iImageArray[(i*iWidth)+j] = 0xFF00FFFF; // actual value of yellow
    } 
} 
新一帅帅 2025-01-15 16:18:31

我还编写了一个程序,可以从图像中读取每个像素的 RGB 颜色。
这是我要阅读的代码:

int orgWidth = bmp.getWidth();
int orgHeight = bmp.getHeight();
//define the array size
int[] pixels = new int[orgWidth * orgHeight];

bmp.getPixels(pixels, 0, orgWidth, 0, 0, orgWidth, orgHeight);

for (int y = 0; y < orgHeight; y++){
    for (int x = 0; x < orgWidth; x++){
    }
}

这就是我的数组的工作原理。
上周我提出了一个关于远程像素检查的问题,但我也自己修复了它:)
使用嵌套 for 循环优化颜色范围检查

编辑:

现在我明白了,抱歉。
位于:iImageArray[j] = 0xFFFFFFFF;
还添加 iImageArray[i] = 0xFFFFFFFF;
那么它应该可以工作

I also made a program that reads the RGB colors of every pixels from an image.
Here is my code to read:

int orgWidth = bmp.getWidth();
int orgHeight = bmp.getHeight();
//define the array size
int[] pixels = new int[orgWidth * orgHeight];

bmp.getPixels(pixels, 0, orgWidth, 0, 0, orgWidth, orgHeight);

for (int y = 0; y < orgHeight; y++){
    for (int x = 0; x < orgWidth; x++){
    }
}

This is how my array works.
I made a question for ranged pixel check last week, however I also fixed it by my self :)
Optimize Color range check with nested for loop

EDIT:

Now I see, sorry.
at: iImageArray[j] = 0xFFFFFFFF;
also add iImageArray[i] = 0xFFFFFFFF;
then it should work

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