Android 编辑位图通道

发布于 2024-10-10 03:59:19 字数 127 浏览 0 评论 0原文

可以使用 extractAlpha() 访问给定位图的 Alpha 通道,但我还没有找到任何方法来实际设置位图的 Alpha 通道。

如何使用 Android 将多个灰度图像作为通道重新组合成位图?

It's possible to access the alpha channel of a given bitmap with extractAlpha(), but I haven't been able to find any way to actually set the alpha channel of a bitmap.

How can multiple greyscale images be recombined as channels into a Bitmap with Android?

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

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

发布评论

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

评论(3

〃安静 2024-10-17 03:59:19

很有可能将单独的通道重新组合回 ARGB 图像。您只需要灰度通道图像和具有您想要的 Alpha 通道的图像 - 请注意,这不是不透明的灰度图像,而是具有您想要的 Alpha 通道的图像。然后,您可以使用适当的 PorterDuffXfermode 使用 Paint 将每个通道绘制到空白的黑色填充位图上。

// have your 3 channel grayscales and 1 alpha bitmap loaded by this point

Paint redPaint = new Paint();
redPaint.setXfermode(new PorterDuffXfermode(Mode.LIGHTEN));
redPaint.setShader(new BitmapShader(redChanImg, TileMode.CLAMP, TileMode.CLAMP));
redPaint.setColorFilter(new PorterDuffColorFilter(Color.RED, Mode.DARKEN));

Paint greenPaint = new Paint();
greenPaint.setXfermode(new PorterDuffXfermode(Mode.LIGHTEN));
greenPaint.setShader(new BitmapShader(greenChanImg, TileMode.CLAMP, TileMode.CLAMP));
greenPaint.setColorFilter(new PorterDuffColorFilter(Color.GREEN, Mode.DARKEN));

Paint bluePaint = new Paint();
bluePaint.setXfermode(new PorterDuffXfermode(Mode.LIGHTEN));
bluePaint.setShader(new BitmapShader(blueChanImg, TileMode.CLAMP, TileMode.CLAMP));
bluePaint.setColorFilter(new PorterDuffColorFilter(Color.BLUE, Mode.DARKEN));

Paint alphaPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
alphaPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

c.setBitmap(resultImage);
c.drawRect(0, 0, width, height, redPaint);
c.drawRect(0, 0, width, height, greenPaint);
c.drawRect(0, 0, width, height, bluePaint);
c.drawBitmap(alphaImg, 0, 0, alphaPaint);

//save off resultImage, display it, etc...

使用上面的代码和以下 4 个图像(分别为红色、绿色、蓝色和 alpha):
替代文本替代文本替代文本 alt text

我们得到以下结果:


alt text


简单说明:红色椭圆形是不透明的,透明背景上的红色椭圆形 - 颜色对此并不重要,但 alpha 却很重要

It is quite possible to re-combine separate channels back into an ARGB image. You just need the grayscale channel images and an image with the alpha channel you want - note that this is not an opaque grayscale image, but an image with the alpha you want. You then draw each channel with a Paint using the appropriate PorterDuffXfermode onto a blank, black-filled Bitmap.

// have your 3 channel grayscales and 1 alpha bitmap loaded by this point

Paint redPaint = new Paint();
redPaint.setXfermode(new PorterDuffXfermode(Mode.LIGHTEN));
redPaint.setShader(new BitmapShader(redChanImg, TileMode.CLAMP, TileMode.CLAMP));
redPaint.setColorFilter(new PorterDuffColorFilter(Color.RED, Mode.DARKEN));

Paint greenPaint = new Paint();
greenPaint.setXfermode(new PorterDuffXfermode(Mode.LIGHTEN));
greenPaint.setShader(new BitmapShader(greenChanImg, TileMode.CLAMP, TileMode.CLAMP));
greenPaint.setColorFilter(new PorterDuffColorFilter(Color.GREEN, Mode.DARKEN));

Paint bluePaint = new Paint();
bluePaint.setXfermode(new PorterDuffXfermode(Mode.LIGHTEN));
bluePaint.setShader(new BitmapShader(blueChanImg, TileMode.CLAMP, TileMode.CLAMP));
bluePaint.setColorFilter(new PorterDuffColorFilter(Color.BLUE, Mode.DARKEN));

Paint alphaPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
alphaPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

c.setBitmap(resultImage);
c.drawRect(0, 0, width, height, redPaint);
c.drawRect(0, 0, width, height, greenPaint);
c.drawRect(0, 0, width, height, bluePaint);
c.drawBitmap(alphaImg, 0, 0, alphaPaint);

//save off resultImage, display it, etc...

With the above code and the following 4 images (red, green, blue, and alpha, respectively):
alt textalt textalt textalt text

We get the following result:


alt text


Just a quick note: the red oval is an opaque, red oval on a transparent background - the color doesn't matter for this one, but the alpha does

梦毁影碎の 2024-10-17 03:59:19

当直接访问像素(字节)时,操作位图是一件非常简单的事情。
要在 Android 中执行此操作,您可以通过此方法完成。

ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos); 
byte[] b = bos.toByteArray();  

现在您可以进行任何您喜欢的图像处理、转换或组合。

我希望这就是您正在寻找的。

Manipulating Bitmaps is a farily simple thing, when to access the pixel (bytes) directly.
To do that in Android you can do it over this approch

ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos); 
byte[] b = bos.toByteArray();  

Now you can do any image manipulation, tranformation or combination you like.

I hope this is what you were looking for.

不爱素颜 2024-10-17 03:59:19

你尝试过用画布吗?下面看起来像是一个 hack,但也许它会起作用。我自己没有测试过。

    Bitmap bitmap;
    int color = bitmap.getPixel(1, 123);
    Rect rect = new Rect(1,123,2,124);
    Canvas c = new Canvas(bitmap);
    c.clipRect(rect);
    c.drawARGB(50, Color.red(color), Color.green(color), Color.blue(color));

Have you tried with canvas? The following looks like a hack, but maybe it will work. I have not tested it myself.

    Bitmap bitmap;
    int color = bitmap.getPixel(1, 123);
    Rect rect = new Rect(1,123,2,124);
    Canvas c = new Canvas(bitmap);
    c.clipRect(rect);
    c.drawARGB(50, Color.red(color), Color.green(color), Color.blue(color));

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