I know how to read a bitmap file into a byte array. How is the byte array then converted to a Java Bitmap?
Skip the byte array if you want:
Bitmap bitmap = BitmapFactory.decodeFile(filename);
Use BitmapFactory if you already have your byte array:
Bitmap bitmap = BitmapFactory.decodeByteArray(yourByteArray, offset, length);
Related
This question already has answers here:
Java - get pixel array from image
(7 answers)
Closed 4 years ago.
I want to read in a bitmap so that I have an array of the 256-RGB values for each pixel.
Currently my code is:
File image = serverConfig.get(map.bmp);
BufferedImage buffer = ImageIO.read(image);
dimX = buffer.getWidth();
dimY = buffer.getHeight();
byte[] pixlesB = (byte[]) buffer.getRaster().getDataElements(0, 0, buffer.getWidth(), buffer.getHeight(), null);
This produces a byte array of the bitmap e.g.
[pixel1Red,pixel1Green,pixel1Blue,pixel2Red,pixel2Green,pixel2Blue,...]
My problem is that when I load a large bitmap (currently the one I'm trying is about 706,000 pixles^2) the bitmap lossless compresses the file and I just get a string of semi-meaningless numbers.
Is there any way to force java to read out the RGB values for all bitmaps, like it does for small ones?
EDIT:
To clarify, I am getting back a [pixel1Red,pixel1Green,pixel1Blue,pixel2Red,pixel2Green,pixel2Blue,...] style list, but the values in there aren't the 0-255 bytes I'm expecting, they're just random, compressed numbers. I need to actual 0-255 values for RGB (or better yet just a byte array of all the hex values) in order for the rest of my code to reliably work.
Try this method:
File image = serverConfig.get(map.bmp);
BufferedImage buffer = ImageIO.read(image);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(buffer, "bmp", baos );
baos.flush();
byte[] pixlesB = baos.toByteArray();
baos.close();
I am trying to create a helper function using OpenCV Java API that would process an input image and return the output byte array. The input image is a jpg file saved in the computer. The input and output image are displayed in the Java UI using Swing.
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// Load image from file
Mat rgba = Highgui.imread(filePath);
Imgproc.cvtColor(rgba, rgba, Imgproc.COLOR_RGB2GRAY, 0);
// Convert back to byte[] and return
byte[] return_buff = new byte[(int) (rgba.total() * rgba.channels())];
rgba.get(0, 0, return_buff);
return return_buff;
When the return_buff is returned and converted to BufferedImage I get NULL back. When I comment out the Imgproc.cvtColor function, the return_buff is properly converted to a BufferedImage that I can display. It seems like the Imgproc.cvtColor is returning a Mat object that I couldn't display in Java.
Here's my code to convert from byte[] to BufferedImage:
InputStream in = new ByteArrayInputStream(inputByteArray);
BufferedImage outputImage = ImageIO.read(in);
In above code, outputImage is NULL
Does anybody have any suggestions or ideas?
ImageIO.read(...) (and the javax.imageio package in general) is for reading/writing images from/to file formats. What you have is an array containing "raw" pixels. It's impossible for ImageIO to determine file format from this byte array. Because of this, it will return null.
Instead, you should create a BufferedImage from the bytes directly. I don't know OpenCV that well, but I'm assuming that the result of Imgproc.cvtColor(rgba, rgba, Imgproc.COLOR_RGB2GRAY, 0) will be an image in grayscale (8 bits/sample, 1 sample/pixel). This is the same format as BufferedImage.TYPE_BYTE_GRAY. If this assumption is correct, you should be able to do:
// Read image to Mat as before
Mat rgba = ...;
Imgproc.cvtColor(rgba, rgba, Imgproc.COLOR_RGB2GRAY, 0);
// Create an empty image in matching format
BufferedImage gray = new BufferedImage(rgba.width(), rgba.height(), BufferedImage.TYPE_BYTE_GRAY);
// Get the BufferedImage's backing array and copy the pixels directly into it
byte[] data = ((DataBufferByte) gray.getRaster().getDataBuffer()).getData();
rgba.get(0, 0, data);
Doing it this way, saves you one large byte array allocation and one byte array copy as a bonus. :-)
I used this kind of code to convert Mat object to Buffered Image.
static BufferedImage Mat2BufferedImage(Mat matrix)throws Exception {
MatOfByte mob=new MatOfByte();
Imgcodecs.imencode(".jpg", matrix, mob);
byte ba[]=mob.toArray();
BufferedImage bi=ImageIO.read(new ByteArrayInputStream(ba));
return bi;
}
I have a ByteArrayOutputStream I have created using the QRGen Qr code generation library, and I want to turn it into a BufferedImage object and present it in an ImageView in Android. How may that be achieved?
Convert the ByteArrayOutputStream to Byte array
byte[] data = baos.toByteArray(); //baos is your ByteArrayOutputStream object
them create a bitmap out of it using the BitmapFactory
Bitmap bmp = BitmapFactory.decodeByteArray (data,0,data.length, null);
them take your ImageView and set its bitmap
imageView.setImageBitmap(bmp);
I use following code for get bitmap from string but i get null bitmap. So please guide me.
byte[] Image_getByte = Base64.decode(img);
ByteArrayInputStream bytes = new ByteArrayInputStream(Image_getByte);
BitmapDrawable bmd = new BitmapDrawable(bytes);
Bitmap bitmap=bmd.getBitmap();
Log.v("log","Home bitmap "+bitmap);
i.setImageBitmap(bitmap);
use BitmapFactory.decodeByteArray(Image_getByte)
here you don't need any String, after getting the byte array, just pass it in the mentioned method which returns Bitmap
The javadoc for getBitmap() says:
"Returns the bitmap used by this drawable to render. May be null."
If img is base 64 String use the following source to get the bitmap from base 64 string.
byte[] source=img.getBytes();
byte[] Image_getByte = Base64.decode(source);
Bitmap bitmap = BitmapFactory.decodeByteArray(Image_getByte, 0,Image_getByte.length);
imageView.setImageBitmap(bitmap);
This may help you
Please consider 'ImageContents' as String which contains image data.
byte[] imageAsBytes = Base64.decode(ImageContents.getBytes());
ImageView image = (ImageView)this.findViewById(R.id.ImageView);
image.setImageBitmap(
BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
);
ImageView :In Android, you can use “android.widget.ImageView” class to
display an image file
BitmapFactory:Creates Bitmap objects from various sources, including
files, streams, and byte-arrays.
For more information about BitmapFactory see here
Let's say I have a byte buffer and how do I get Bitmap?
If you want to manipulate the image, use ImageIO. It creates a format-agnostic image in memory:
BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));
If you just want to store in to disk, then simply write the byte array to a file.
Note that the byte array must be already a bitmap image, you can't take any byte array and have it as bitmap.
And you get the byte array from the byte buffer by:
byte[] bytes = new byte[buf.capacity()];
buf.get(bytes, 0, bytes.length);