簡體   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