Code example for Bitmap

Methods: getHeightgetPixelsgetWidthrecycle

0
    public static ImageDecoder decodeFromBitmap(Bitmap image, boolean recycle) {
        if (image == null) {
            return null; 
        } 
 
        final int image_width = image.getWidth();
        final int image_height = image.getHeight();
        ImageDecoder result = new ImageDecoder();
 
        // ピクセル情報の格納先を確保 
        ByteBuffer pixelBuffer = ByteBuffer.allocateDirect(image_width * image_height * 4);
        { 
            result.width = image_width;
            result.height = image_height;
            result.pixels = pixelBuffer;
        } 
 
        AndroidUtil.log(String.format("image size(%d x %d)", image_width, image_height));
 
        final int[] temp = new int[image_width];
        final byte[] pixel_temp = new byte[4];
        for (int i = 0; i < image_height; ++i) {
            // 1ラインずつ読み込む 
            image.getPixels(temp, 0, image_width, 0, i, image_width, 1);
            // 結果をByteArrayへ書き込む 
            for (int k = 0; k < image_width; ++k) {
                final int pixel = temp[k];
 
                pixel_temp[0] = (byte) ((pixel >> 16) & 0xFF);
                pixel_temp[1] = (byte) ((pixel >> 8) & 0xFF);
                pixel_temp[2] = (byte) ((pixel) & 0xFF);
                pixel_temp[3] = (byte) ((pixel >> 24) & 0xFF);
 
                pixelBuffer.put(pixel_temp);
            } 
        } 
 
        // 書き込み位置をリセットする 
        pixelBuffer.position(0);
 
        if (recycle) {
            image.recycle();
        } 
        return result;
    } 
 
    /** 
Connect your IDE to all the code out there  Get Codota for Java