Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9218497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:52:41+00:00 2026-06-18T02:52:41+00:00

In my application, I am getting the Image from the server. Now i want

  • 0

In my application, I am getting the Image from the server. Now i want to convert that Image to Bitmap for displaying in the screen. For that i am using below codes. But it is not giving me the same image means not with exact dimension and clarity. It is coming smaller than the exact image.

I have used the below codes:

private Bitmap getBitmapFromImg(Image img) {
    Bitmap bmp = null;
    try {
        Logger.out(TAG, "It is inside the the image conversion        " +img);
        Image image = Image.createImage(img);
        byte[] data = BMPGenerator.encodeBMP(image);
        Logger.out(TAG, "It is inside the the image conversion---333333333"+data);
        bmp = Bitmap.createBitmapFromBytes(data, 0, data.length, 1);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bmp;
}

Here is the BMPGenerator class:

public final class BMPGenerator {

    /**
     * @param image
     * @return
     * @throws IOException
     * @see {@link #encodeBMP(int[], int, int)}
     */
    public static byte[] encodeBMP(Image image) throws IOException {
            int width = image.getWidth();
            int height = image.getHeight();
            int[] rgb = new int[height * width];
            image.getRGB(rgb, 0, width, 0, 0, width, height);
            return encodeBMP(rgb, width, height);
    }

    /**
     * A self-contained BMP generator, which takes a byte array (without any unusual
     * offsets) extracted from an {@link Image}. The target platform is J2ME. You may
     * wish to use the convenience method {@link #encodeBMP(Image)} instead of this.
     * <p>
     * A BMP file consists of 4 parts:-
     * <ul>
     * <li>header</li>
     * <li>information header</li>
     * <li>optional palette</li>
     * <li>image data</li>
     * </ul>
     * At this time only 24 bit uncompressed BMPs with Windows V3 headers can be created.
     * Future releases may become much more space-efficient, but will most likely be
     * ditched in favour of a PNG generator.
     * 
     * @param rgb
     * @param width
     * @param height
     * @return
     * @throws IOException
     * @see http://en.wikipedia.org/wiki/Windows_bitmap
     */
    public static byte[] encodeBMP(int[] rgb, int width, int height)
                    throws IOException {
            int pad = (4 - (width % 4)) % 4;
            // the size of the BMP file in bytes
            int size = 14 + 40 + height * (pad + width * 3);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream(size);
            DataOutputStream stream = new DataOutputStream(bytes);
            // HEADER
            // the magic number used to identify the BMP file: 0x42 0x4D
            stream.writeByte(0x42);
            stream.writeByte(0x4D);
            stream.writeInt(swapEndian(size));
            // reserved
            stream.writeInt(0);
            // the offset, i.e. starting address of the bitmap data
            stream.writeInt(swapEndian(14 + 40));
            // INFORMATION HEADER (Windows V3 header)
            // the size of this header (40 bytes)
            stream.writeInt(swapEndian(40));
            // the bitmap width in pixels (signed integer).
            stream.writeInt(swapEndian(width));
            // the bitmap height in pixels (signed integer).
            stream.writeInt(swapEndian(height));
            // the number of colour planes being used. Must be set to 1.
            stream.writeShort(swapEndian((short) 1));
            // the number of bits per pixel, which is the colour depth of the image.
            stream.writeShort(swapEndian((short) 24));
            // the compression method being used.
            stream.writeInt(0);
            // image size. The size of the raw bitmap data. 0 is valid for uncompressed.
            stream.writeInt(0);
            // the horizontal resolution of the image. (pixel per meter, signed integer)
            stream.writeInt(0);
            // the vertical resolution of the image. (pixel per meter, signed integer)
            stream.writeInt(0);
            // the number of colours in the colour palette, or 0 to default to 2n.
            stream.writeInt(0);
            // the number of important colours used, or 0 when every colour is important;
            // generally ignored.
            stream.writeInt(0);
            // PALETTE
            // none for 24 bit depth
            // IMAGE DATA
            // starting in the bottom left, working right and then up
            // a series of 3 bytes per pixel in the order B G R.
            for (int j = height - 1; j >= 0; j--) {
                    for (int i = 0; i < width; i++) {
                            int val = rgb[i + width * j];
                            stream.writeByte(val & 0x000000FF);
                            stream.writeByte((val >>> 8) & 0x000000FF);
                            stream.writeByte((val >>> 16) & 0x000000FF);
                    }
                    // number of bytes in each row must be padded to multiple of 4
                    for (int i = 0; i < pad; i++) {
                            stream.writeByte(0);
                    }
            }
            byte[] out = bytes.toByteArray();
            bytes.close();
            // quick consistency check
            if (out.length != size)
                    throw new RuntimeException("bad math");
            return out;
    }

    /**
     * Swap the Endian-ness of a 32 bit integer.
     * 
     * @param value
     * @return
     */
    private static int swapEndian(int value) {
            int b1 = value & 0xff;
            int b2 = (value >> 8) & 0xff;
            int b3 = (value >> 16) & 0xff;
            int b4 = (value >> 24) & 0xff;

            return b1 << 24 | b2 << 16 | b3 << 8 | b4 << 0;
    }

    /**
     * Swap the Endian-ness of a 16 bit integer.
     * 
     * @param value
     * @return
     */
    private static short swapEndian(short value) {
            int b1 = value & 0xff;
            int b2 = (value >> 8) & 0xff;

            return (short) (b1 << 8 | b2 << 0);
    }

Where is wrong in my code?? Is there any other way to do the same??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:52:42+00:00Added an answer on June 18, 2026 at 2:52 am

    I believe you don’t need this part:

    Image image = Image.createImage(img);
    byte[] data = BMPGenerator.encodeBMP(image);
    Logger.out(TAG, "It is inside the the image conversion---333333333"+data);
    bmp = Bitmap.createBitmapFromBytes(data, 0, data.length, 1);
    

    Just use Image to get the raw ARGB data:

    int width = image.getWidth();
    int height = image.getHeight();
    int[] argbData = new int[height * width];
    image.getRGB(argbData, 0, width, 0, 0, width, height);
    

    Then use argbData to create a Bitmap:

    Bitmap b = new Bitmap(width, height);
    b.setARGB(argbData, 0, width, 0, 0, width, height);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When i'm displaying image from url with using below code, application is taking too
my application gets image from clipboard and saves it to server. getting image is
I have an application that is loading html from a remote server in a
I have a php application getting x and y position constantly from the server.
I am getting image and some details from .net web server. I need to
In my application I have an image viewer and I am getting bitmaps from
I want to make a fairly straightforward image link that uses an image from
I have an application that shows a screen of image thumbnails, each image is
I am developed a GPS application for getting Latitude and Longitude, by using Emulator
I have an ipad app which takes images from Photos application using ALAssetsLibrary and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.