如何在Android' java'中将Bitmap对象转换为Image对象,反之亦然

时间:2015-01-22 20:54:29

标签: java android image bitmap

如何将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;
}

2 个答案:

答案 0 :(得分:1)

如果要将Image对象转换为Bitmap并且格式已选择为JPEG,则可以使用以下代码完成此操作(如果它不是JPEG,则需要进行其他转换):< / p>

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

答案 1 :(得分:0)

link更多地将图像保存为png格式。

很难看到您尝试做什么,您是否尝试更改此代码以使其也适用于位图格式?

这里有一个answer某人做位图图像的东西,应该让你知道其他人做什么