gpt4 book ai didi

java - 如何在 android 'java' 中将位图对象转换为图像对象,反之亦然

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:38:08 26 4
gpt4 key购买 nike

如何将 Image obj 转换为 Bitmap obj,反之亦然?


我有一个获取 Image 对象输入并返回 Image 对象的方法,但我想提供位图对象输入,然后获取位图对象输出我的代码是这样的:


public Image edgeFilter(Image imageIn) {
// Image size
int width = imageIn.getWidth();
int height = imageIn.getHeight();
boolean[][] mask = null;
Paint grayMatrix[] = new Paint[256];

// Init gray matrix
for (int i = 0; i <= 255; i++) {
Paint p = new Paint();
p.setColor(Color.rgb(i, i, i));
grayMatrix[i] = p;
}
int [][] luminance = new int[width][height];
for (int y = 0; y < height ; y++) {
for (int x = 0; x < width ; x++) {
if(mask != null && !mask[x][y]){
continue;
}
luminance[x][y] = (int) luminance(imageIn.getRComponent(x, y), imageIn.getGComponent(x, y), imageIn.getBComponent(x, y));
}
}
int grayX, grayY;
int magnitude;
for (int y = 1; y < height-1; y++) {
for (int x = 1; x < width-1; x++) {

if(mask != null && !mask[x][y]){
continue;
}

grayX = - luminance[x-1][y-1] + luminance[x-1][y-1+2] - 2* luminance[x-1+1][y-1] + 2* luminance[x-1+1][y-1+2] - luminance[x-1+2][y-1]+ luminance[x-1+2][y-1+2];
grayY = luminance[x-1][y-1] + 2* luminance[x-1][y-1+1] + luminance[x-1][y-1+2] - luminance[x-1+2][y-1] - 2* luminance[x-1+2][y-1+1] - luminance[x-1+2][y-1+2];

// Magnitudes sum
magnitude = 255 - Image.SAFECOLOR(Math.abs(grayX) + Math.abs(grayY));
Paint grayscaleColor = grayMatrix[magnitude];

// Apply the color into a new image
imageIn.setPixelColor(x, y, grayscaleColor.getColor());
}
}

return imageIn;
}

最佳答案

如果你想将一个Image对象转换成一个Bitmap,并且格式选择为JPEG,那么你可以使用下面的代码来完成(如果它不是JPEG,那么需要额外的转换):

...
if(image.getFormat() == ImageFormat.JPEG)
{
ByteBuffer buffer = capturedImage.getPlanes()[0].getBuffer();
byte[] jpegByteData = new byte[buffer.remaining()];
Bitmap bitmapImage = BitmapFactory.decodeByteArray(jpegByteData, 0, jpegByteData.length, null);
}
...

关于java - 如何在 android 'java' 中将位图对象转换为图像对象,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28098499/

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