[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

5.8

create a BufferedImage from a byte[] (translate C++ code)

Asked by spray07 in Java Programming Language, Microsoft Visual C++.Net

Tags: c++, java, bufferedimage, colorModel

I know that such questions have been asked so many times. But no one I can use. Sorry for that.
I need to translate some code from c++  to java. Source code  on c++  create image from byte array (code in attachment).  I need  write code, that convert byte[] to BufferedImage.

I use samples from other questions, but its no work properly. In code you may see  4 functions I write. Its not work.

How I think from c++ code,  the RAW image data (byte array)  contain 256 color image (i byte per pixel) , but i cant initialize it in java.   Image  get from FINGERPRINT SCANER.
p.s. Sorry for my bad english
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
//dont work
public static BufferedImage toImage(int w, int h, byte[] data) {
    DataBuffer buffer = new DataBufferByte(data, 4614400);
 
    int pixelStride = 4; //assuming r, g, b, skip, r, g, b, skip...
    int scanlineStride = 4*w; //no extra padding
    int[] bandOffsets = {0, 1, 2}; //r, g, b
    WritableRaster raster = Raster.createInterleavedRaster(buffer, w, h, scanlineStride, pixelStride, bandOffsets, null);
    ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    boolean hasAlpha = false;
    boolean isAlphaPremultiplied = false;
    int transparency = Transparency.OPAQUE;
    int transferType = DataBuffer.TYPE_BYTE;
    ColorModel colorModel = new ComponentColorModel(colorSpace, hasAlpha, isAlphaPremultiplied, transparency, transferType);
 
    return new BufferedImage(colorModel, raster, isAlphaPremultiplied, null);
}
 
 
//NEW VARIANT
public static BufferedImage toImage1(BufferedImage bi, int w, int h, byte[] data) {
    DataBuffer buffer = new DataBufferByte(data, w*h);
    WritableRaster   biR =  bi.getRaster();
     boolean isAlphaPremultiplied = bi.isAlphaPremultiplied();
    int pixelStride = 4; //assuming r, g, b, skip, r, g, b, skip...
    int scanlineStride = 4*w; //no extra padding
    int[] bandOffsets = {0, 1, 2}; //r, g, b
//    WritableRaster raster =Raster.createPackedRaster(buffer, w, h, scanlineStride, pixelStride, bandOffsets, null);
    WritableRaster raster =Raster.createPackedRaster(buffer, w, h, 8, null);
 
//    biR.createRaster(sm, buffer, location)
    ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    boolean hasAlpha = false;
//    boolean isAlphaPremultiplied = false;
    int transparency = bi.getTransparency();// Transparency.OPAQUE;
    int transferType = bi.getType();//DataBuffer.TYPE_BYTE;
    ColorModel colorModel = new ComponentColorModel(colorSpace, hasAlpha, isAlphaPremultiplied, transparency, transferType);
 
    return new BufferedImage(colorModel, raster, isAlphaPremultiplied, null);
}
 
    public static BufferedImage toImage3( int w, int h,byte[] buf){
        BufferedImage frame=null;
        String sBuf = new String(buf);
        int bufLength=sBuf.length();
        int alpha=255;
        int red=0;
        int green=0;
        int blue=0;
        int iBuf=0;
        int pIndex=0;
        int[] pixels= new int[bufLength];
/*  // This code from other sample, but  that sample have 3 byte for pixel
        for(int col= h -1 ;col >=0;col--){
            iBuf= w*3*col;
            for(int row = 0;row < w;row++){
                alpha=255;
                red=0;
                green=0;
                blue=0;
                blue    |= (buf[iBuf++] & 0xff );
                green   |= (buf[iBuf++] & 0xff );
                red     |= (buf[iBuf++] & 0xff );
                pixels[pIndex]=(alpha << 24) |  (red << 16) |  (green << 8 ) | blue;
                pIndex++;
            }
        }
*/
        DataBuffer buffer = new DataBufferByte(buf, w*h);
        WritableRaster raster =Raster.createPackedRaster(buffer, w, h, 8, null);
 
        frame = new BufferedImage(w,h,BufferedImage.TYPE_BYTE_INDEXED);
//        frame.setRGB(0, 0, w, h,byte2int(buf,buf.length), 0, w);
        frame.setData(raster);
        
        return frame;
        
    }
 
// SECOND VARIANT
public static BufferedImage toImage2( int w, int h, byte[] data) {
 
         // create ColorModel using my method
           ColorModel colorModel = makeDefaultColorModel();
 
        //reconstruct bufferedimage from data,w,h
                // get sample model from color model
           SampleModel sampleModel = colorModel.createCompatibleSampleModel(w, h);
 
         // make raster using dbuf which I created using a given byte[] of RAW data
         DataBuffer resultBuffer = new DataBufferInt(byte2int(data,data.length), data.length);
 
           WritableRaster raster = Raster.createWritableRaster(sampleModel, resultBuffer, null);
 
       // make image
          image = new BufferedImage(colorModel, raster, false, null);
 
    return image;
}
 
 public static int[] byte2int( byte[] data, int  size) {
        int[] int_arr= new int[size];
        for (int i=0; i<size; i++){
//            int_arr[i] = (data[i] & 0xff );
              int_arr[i] = (int) data[i];
        }
        return int_arr;
}
 
 
        static ColorModel makeDefaultColorModel()
        {
                byte[] r = new byte[256];
                byte[] g = new byte[256];
                byte[] b = new byte[256];
               /* for(int i=0; i<256; i++)
                {
                        r[i]=(byte)i;
                        g[i]=(byte)i;
                        b[i]=(byte)i;
                }
                return new IndexColorModel(8, 256, r, g, b);
 
                */
          return ColorModel.getRGBdefault();
        }
[+][-]10/28/09 06:25 AM, ID: 25682958Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/28/09 07:01 AM, ID: 25683291Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/28/09 07:12 AM, ID: 25683397Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/28/09 07:34 AM, ID: 25683662Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/28/09 08:18 AM, ID: 25684193Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/28/09 09:37 AM, ID: 25685178Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/28/09 09:40 AM, ID: 25685205Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/28/09 09:42 AM, ID: 25685234Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/28/09 10:14 AM, ID: 25685623Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/28/09 10:19 AM, ID: 25685692Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/28/09 10:25 AM, ID: 25685834Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Java Programming Language, Microsoft Visual C++.Net
Tags: c++, java, bufferedimage, colorModel
Sign Up Now!
Solution Provided By: CEHJ
Participating Experts: 1
Solution Grade: B
 
[+][-]10/29/09 05:33 AM, ID: 25692873Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/29/09 05:34 AM, ID: 25692875Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89 - Hierarchy / EE_QW_3_20080625