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);
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 working on a project where I need to first convert video to byte array to reduce its size and then convert it back to video.
I'm not sure that you will reduce a video's size by putting each byte into an array.
If I have a video that is 4,000 bytes then you will have an array of exactly 4,000 bytes (including all of the overhead of the array itself in memory).
There is a method on a ParseFile object called .getData() that will return a byte[] array. So if you have a video stored in Video class in the data column you can do something like this:
byte[] bytes_of_video = videoObject.getParseFile("data").getBytes();
Now you have a byte array of the video object but the byte array will be just as big as the video file since no compression took place. If you want to compress the video file you can use Android's zlib compression with Deflater:
byte[] originalBytes = bytes_of_video;
Deflater deflater = new Deflater();
deflater.setInput(originalBytes);
deflater.finish();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[8192];
while (!deflater.finished()) {
int byteCount = deflater.deflate(buf);
baos.write(buf, 0, byteCount);
}
deflater.end();
byte[] compressedBytes = baos.toByteArray();
I don't know if you are wanting to save space on the server or on the device but to save bandwidth costs you could store the compressed video data in your ParseFile object in the server and then decompress the file on the device when it's ready to be viewed/used. But a compressed video file on the device is not very useful since you won't be able to view/edit/use it while it's in a compressed format.
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 am trying to convert byte[] array to buffered image so than i can resize the image..but problem is conversion always turned into null.here is my code..
ByteArrayInputStream bais = new ByteArrayInputStream(user.getUser_image());
//Here user.getUser_image() returns byte[] returned from server..
try {
BufferedImage image = ImageIO.read(bais);
System.out.println("============><================"+image);//Here it prints null
BufferedImage scaledImage = Scalr.resize(image,48);
}
.....and so on
It means that the ImageIO class is not able to select an appropriate ImageReader. The purpose of this could be corrupted byte array or unsupported image type. Try to debug it.
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);