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

标签 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;
}

最佳答案

如果你想将一个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/

相关文章:

android - Google Plus 应用程序(流)使用什么 View 组件?

html - css如何只显示背景的一部分?

java - 类似于 Couchbase 中的 SQL LIKE 语句

java - 在达到特定请求阈值之前,是否由 Spring Boot 中的单个线程处理多个请求?

java - Cassandra 无法在 OSX 上启动 "Reason: Connection refused."

android - 使用 Kotlin 在 Firestore 数据库中上传数据。无法存储数据

android - Android 的 PDF 库?

image - golang api 图像下载中的图像文件损坏

css - 如何在CSS中保持图像的纵横比?

java - 运行Applet后没有任何显示