gpt4 book ai didi

java - 位图转换 : Creating bitmap that excludes transparent sides from transparent bitmap

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:02:55 30 4
gpt4 key购买 nike

我有一组位图。它们在某种程度上都是透明的,我事先不知道哪些部分是透明的。我想从排除透明部分但在正方形中的原始位图中创建一个新位图。我认为这张图片解释了这一点:

enter image description here

我知道如何从现有位图中创建位图,但我不知道如何找出透明部分以及如何使用它来实现我的目标。

这就是我打算这样做的方式:

public Bitmap cutImage(Bitmap image) {
Bitmap newBitmap = null;

int width = image.getWidth();
int height = image.getHeight();

newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(newBitmap);

//This is where I need to find out correct values of r1 and r1.

Rect r1 = new Rect(?, ?, ?, ?);
Rect r2 = new Rect(?, ?, ?, ?);

canvas.drawBitmap(image, r1, r2, null);

return newBitmap;
}

有人知道如何实现吗?

编辑:

我使用以下算法找到左值、右值、上值和下值:

private int x1;
private int x2;
private int y1;
private int y2;

private void findRectValues(Bitmap image)
{
for(int x = 0; x < image.getWidth(); x++)
{
for(int y = 0; y < image.getHeight(); y++)
{
if(image.getPixel(x, y) != Color.TRANSPARENT)
{
System.out.println("X1 is: " + x);
x1 = x;
break;
}
}

if(x1 != 0)
break;

}

for(int x = image.getWidth()-1; x > 0; x--)
{
for(int y = 0; y < image.getHeight(); y++)
{
if(image.getPixel(x, y) != Color.TRANSPARENT)
{
System.out.println("X2 is: " + x);
x2 = x;
break;
}
}

if(x2 != 0)
break;

}

for(int y = 0; y < image.getHeight(); y++)
{
for(int x = 0; x < image.getWidth(); x++)
{
if(image.getPixel(x, y) != Color.TRANSPARENT)
{
System.out.println("Y1 is: " + y);
y1 = y;
break;
}
}

if(y1 != 0)
break;

}

for(int y = image.getHeight()-1; y > 0; y--)
{
for(int x = 0; x < image.getWidth(); x++)
{
if(image.getPixel(x, y) != Color.TRANSPARENT)
{
System.out.println("Y2 is: " + y);
y2 = y;
break;
}
}

if(y2 != 0)
break;

}
}

最佳答案

我觉得这样效率更高一些,对我来说效果很好

public Bitmap cropBitmapToBoundingBox(Bitmap picToCrop, int unusedSpaceColor) {
int[] pixels = new int[picToCrop.getHeight() * picToCrop.getWidth()];
int marginTop = 0, marginBottom = 0, marginLeft = 0, marginRight = 0, i;
picToCrop.getPixels(pixels, 0, picToCrop.getWidth(), 0, 0,
picToCrop.getWidth(), picToCrop.getHeight());

for (i = 0; i < pixels.length; i++) {
if (pixels[i] != unusedSpaceColor) {
marginTop = i / picToCrop.getWidth();
break;
}
}

outerLoop1: for (i = 0; i < picToCrop.getWidth(); i++) {
for (int j = i; j < pixels.length; j += picToCrop.getWidth()) {
if (pixels[j] != unusedSpaceColor) {
marginLeft = j % picToCrop.getWidth();
break outerLoop1;
}
}
}

for (i = pixels.length - 1; i >= 0; i--) {
if (pixels[i] != unusedSpaceColor) {
marginBottom = (pixels.length - i) / picToCrop.getWidth();
break;
}
}

outerLoop2: for (i = pixels.length - 1; i >= 0; i--) {
for (int j = i; j >= 0; j -= picToCrop.getWidth()) {
if (pixels[j] != unusedSpaceColor) {
marginRight = picToCrop.getWidth()
- (j % picToCrop.getWidth());
break outerLoop2;
}
}
}

return Bitmap.createBitmap(picToCrop, marginLeft, marginTop,
picToCrop.getWidth() - marginLeft - marginRight,
picToCrop.getHeight() - marginTop - marginBottom);
}

关于java - 位图转换 : Creating bitmap that excludes transparent sides from transparent bitmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6717433/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com