Create a user-defined BufferedImage and image bufferedimage in pixel format based on existing image data

Source: Internet
Author: User

Create a user-defined BufferedImage and image bufferedimage in pixel format based on existing image data

In a recent project, you need to capture camera images in Mac OS X and convert them to BufferedImage objects in Java. First, a local library is developed to capture images from Mac OS X cameras, using the new AVFoundation framework recommended by Apple, the camera image format is set to kCVPixelFormatType_32ARGB (the image cannot be obtained after being set to another test, which is not supported by the system). After the sample buffer of the CMSampleBufferRef type is obtained through the delegate method, you need to use the CMSampleBufferGetImageBuffer function to convert it to the CVImageBufferRef type image buffer (because the captured image data is not the sample data, so the CMSampleBufferGetDataBuffer cannot be used ). Then, use the CVPixelBufferGetBaseAddress function to obtain the first address of the image buffer. Use the CVPixelBufferGetDataSize function to obtain the data size of the image buffer, do not assume that the obtained image buffer data can be directly filled with BufferedImage in Java by using Raster and DataBuffer in Java (Here we assume that BufferedImage uses TYPE_INT_ARGB because it corresponds to kCVPixelFormatType_32ARGB ). If we do this, we will find that the color of the image is completely disordered. In fact, we can know through computation, the data size of the BufferedImage image corresponding to the TYPE_INT_ARGB format under the height and width is width × height × 4 bytes (because one pixel is int type, 4 bytes ), however, the image data size obtained by CVPixelBufferGetDataSize is four bytes more than that obtained by the former, and some other information may be saved. Therefore, it is not feasible to directly create a BufferedImage in TYPE_INT_ARGB format and then fill it with Raster and DataBuffer.

Here we need to use another method to build BufferedImage with DataBufferByte, ComponentSampleModel, WritableRaster, ColorSpace, and ColorModel, in fact, BufferedImage is another infrequent but highly efficient constructor mode. Of course, the premise is that you also need to use the CVPixelBufferGetBytesPerRow function to get the number of bytes for each scanned row in the image. You can use the CVPixelBufferGetHeight function to get the image height, which will be used later. The Code is as follows:

1 DataBufferByte dataBufferByte = new DataBufferByte (new byte [] [] {dataBytes}, dataSize); // assume that dataBytes stores the image data obtained locally, dataSize is the image data size (always greater than the "w * h * number of bytes per pixel") 2 ComponentSampleModel componentSampleModel = new ComponentSampleModel (DataBuffer. TYPE_BYTE, width, height, 4, bytesPerRow, new int [] {1, 2, 3, 0}); // customize the image format in BufferedImage, or store each pixel in bytes. For detailed constructor descriptions, see javadoc api3 WritableRaster writableRaster = Raster. createWritableRaster (componentSampleModel, dataBufferByte, new Point (0, 0); // create a grid array containing specific image data 4 ColorSpace colorSpace = ColorSpace. getInstance (ColorSpace. CS_sRGB); // create an RGB Color Space of 5 int [] nBits = {8, 8, 8, 8}; // ARGB corresponding to the source image data, the source image data uses 32 ARGB, which is equivalent to four bytes. Each byte represents alpha, red, green, and blue6 ColorModel colorModel = new ComponentColorModel (colorSpace, nBits, true, false, Transparency. TRANSLUCENT, DataBuffer. TYPE_BYTE); // create color mode 7 BufferedImage bufferedImage = new BufferedImage (colorModel, writableRaster, false, null); // construct a BufferedImage in a custom pixel format

This method will not cause problems. The real-time camera images captured by the local library can be correctly filled into the Java BufferedImage, and the efficiency is very high.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.