Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-07-2008, 02:55 PM
Member
 
Join Date: Nov 2007
Posts: 13
shaungoater is on a distinguished road
Image class (getRGB)
I am trying to use the following method to return the number of red blue and green pixels contained in a part of an image:

Code:
public int[] getRGB(int startX,int startY,int w, int h,int[] rgbArray, int offset,int scansize);

However i don't understand what all the parameters are for. Specifically the offset and scansize. Also, for example, if i need to split an image 9*9 (81 parts) do i need to call this method 81 times and set up 81 different arrays to store the rgb data.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-07-2008, 10:47 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
don't understand what all the parameters are for. Specifically the offset and scansize
Depends on what you're doing. Seems like we just have to play around with it a little to see how it's working. I tried to make up a small example below.
if i need to split an image 9*9 (81 parts) do i need to call this method 81 times and set up 81 different arrays to store the rgb data.
You could do it that way. Or you could get all of the image data in a single rgbArray and use some clever indexing to access the data you want from the array, storing it in a smaller block-size array as you go.
Code:
import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import javax.swing.*; public class BlockTest { BufferedImage image; JLabel left; JLabel right; int cols = 9; int rows = 9; public void setImage(BufferedImage image) { right.setIcon(new ImageIcon(image)); } private JPanel getContent() { left = getLeft(); new CellSelector(this); right = new JLabel((ImageIcon)null, JLabel.CENTER); right.setPreferredSize(left.getPreferredSize()); JPanel panel = new JPanel(new GridBagLayout()); panel.setBackground(Color.black); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(5,5,5,5); gbc.weightx = 1.0; panel.add(left, gbc); panel.add(right, gbc); return panel; } private JLabel getLeft() { int w = 360; int h = 360; int type = BufferedImage.TYPE_INT_RGB; image = new BufferedImage(w, h, type); Graphics2D g2 = image.createGraphics(); g2.setPaint(new GradientPaint(0,0,Color.green.darker(), w/4,h/4,Color.red, true)); g2.fillRect(0,0,w,h); g2.setPaint(Color.blue); for(int j = 0; j <= rows; j++) { int y = j*(w/rows); g2.drawLine(0,y,h,y); } for(int j = 0; j <= cols; j++) { int x = j*(h/rows); g2.drawLine(x,0,x,h); } g2.dispose(); return new JLabel(new ImageIcon(image), JLabel.CENTER); } public static void main(String[] args) { BlockTest test = new BlockTest(); JFrame f = new JFrame("Click any cell"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test.getContent()); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } } class CellSelector extends MouseAdapter { BlockTest view; JLabel target; int[] rgbArray; CellSelector(BlockTest bt) { view = bt; target = view.left; target.addMouseListener(this); // Create rgbArray with same size as left image. int w = view.image.getWidth(); int h = view.image.getHeight(); rgbArray = new int[w*h]; } public void mousePressed(MouseEvent e) { Point p = e.getPoint(); // Locate origin of centered image inside left label. int targetWidth = target.getWidth(); int targetHeight = target.getHeight(); int imageWidth = view.image.getWidth(); int imageHeight = view.image.getHeight(); int x0 = (targetWidth - imageWidth)/2; int y0 = (targetHeight - imageHeight)/2; // Make sure mouse selection is over image. // User may have changed the layout or something. Rectangle r = new Rectangle(x0, y0, imageWidth, imageHeight); if(!r.contains(p)) return; // Size of each cell in grid. int cellWidth = imageWidth/view.cols; int cellHeight = imageHeight/view.rows; // Indices of row and column selected by the user. int cellColIndex = (p.x - x0)/cellWidth; int cellRowIndex = (p.y - y0)/cellHeight; //System.out.printf("cellRowIndex = %d cellColIndex = %d%n", // cellRowIndex, cellColIndex); // Selected location in image coordinate system - not used. int imageX = p.x - x0; int imageY = p.y - y0; // Origin of selected cell. int x = cellColIndex * cellWidth; int y = cellRowIndex * cellHeight; // Collect the rgb data for the selected // cell and write it into rgbArray at the // cell location (offset) within the array. // ******** Two options: ******** // Write directly into the rgbArray. writeToArray(x, y, cellWidth, cellHeight, imageWidth); // Get the data and transfer it into the rgbArray. //transferToArray(x, y, cellWidth, cellHeight, imageWidth); // ******************************** // Set rgbArray data into a new image and // mount the image in the right label. BufferedImage image = new BufferedImage(imageWidth, imageHeight, view.image.getType()); image.setRGB(0, 0, imageWidth, imageHeight, rgbArray, 0, imageWidth); view.setImage(image); } /** Write data directly into rgbArray. */ private void writeToArray(int x, int y, int w, int h, int imageWidth) { // Calculate offset into rgbArray to begin // writing the rgb data of this cell. int offset = y*imageWidth + x; int scansize = imageWidth; view.image.getRGB(x, y, w, h, rgbArray, offset, scansize); } /** Extract cell block data and write it into rgbArray. */ private void transferToArray(int x, int y, int w, int h, int imageWidth) { int offset = 0; int scansize = w; int[] rgbs = view.image.getRGB(x, y, w, h, null, offset, scansize); // Write the cell data into rgbArray. int count = 0; for(int j = y; j < y+h; j++) { for(int k = x; k < x+w; k++) { int index = j*imageWidth + k; rgbArray[index] = rgbs[count++]; } } } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the Priority for execution of Interface class and a Abstract class Santoshbk Advanced Java 0 04-02-2008 08:04 AM
An example of accessing outer class from inner class Java Tip Java Tips 0 02-17-2008 10:01 AM
Inner class accessing outer class Java Tip Java Tips 0 02-17-2008 09:59 AM
Converting multiple banded image into single banded image... Image enhancement archanajathan Advanced Java 0 01-08-2008 06:29 PM
Accessing one class from another class through swing kbyrne AWT / Swing 5 01-03-2008 08:54 AM


All times are GMT +3. The time now is 11:38 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org