Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How convert Image obj to Bitmap obj and vice versa?


I have a method that get Image object input and return Image object but i want give bitmap object input and then get bitmap object output my code is this:


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;
}
like image 690
Pouria golshanrad Avatar asked Jan 22 '15 20:01

Pouria golshanrad


1 Answers

If you want to convert an Image object to a Bitmap and the format has been selected as JPEG, then you can accomplish this by using the following code (if it is not a JPEG, then additional conversions will be needed):

...
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);
 }
 ...
like image 163
Jay Snayder Avatar answered Oct 16 '22 02:10

Jay Snayder