繁体   English   中英

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

[英]How convert Bitmap Object to Image Object and vice versa in android 'java'

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

链接提供了有关以png格式保存图像的更多信息。

你很难看到你想要做什么,你是否试图改变这个代码,所以它也适用于位图格式?

这里是某人用位图图片做事的答案 ,应该让你知道其他人做了什么

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM