User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 456,472 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,778 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 341 | Replies: 1
Reply
Join Date: Oct 2008
Posts: 1
Reputation: Nina* is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Nina* Nina* is offline Offline
Newbie Poster

2d array to image

  #1  
Oct 1st, 2008
I.ve problem with java image processing.I've to convert 2d array into image.I'm using the loadPixels function, but it doesen't work.The Image is never shown on the label.
Please help, here is the code of the loadPixels function:


public static void loadPixels(JLabel label, int[][] pixels) {
		// 1. convert to 1-d array
		int width  = pixels.length;
		int height = pixels[0].length; 
		int i=0;    // index into new pixarray
		int[] pixarray = new int[width*height];
		for (int y=0; y<height; y++)
		for (int x=0; x<width;  x++)
			// these are stored y-major!
			pixarray[i++] = pixels[x][y];
		// 2. convert 1-d array into Image*/
		MemoryImageSource source = new MemoryImageSource(width,height,pixarray,0,width);
		
	 imageto=label.createImage(source);   // imageto is declared in the top of the class as "private static Image"
		
		// 3. store Image in the JLabel
		ImageIcon imic=new ImageIcon(imageto);
		
		//imic=image;
		label.setIcon(imic);		
	} 

Any help would be greatly appreciated.

Thanks,

Nina
Last edited by Nina* : Oct 1st, 2008 at 8:29 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,090
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 307
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: 2d array to image

  #2  
Oct 1st, 2008
It may simply be a matter of incorrectly specifying the pixel values, because when I use this code (from the example in the MemoryImageSource API)
        for (int y = 0; y < height; y++) {
            int red = (y * 255) / (height - 1);
            for (int x = 0; x < width; x++) {
                int blue = (x * 255) / (width - 1);
                pixarray[i++] = (255 << 24) | (red << 16) | blue;
            }
        }
in place of your loop to fill 'pixarray' from the pixels param, it works just fine.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 2:33 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC