Click here to Skip to main content
15,891,529 members
Home / Discussions / Java
   

Java

 
QuestionHow to get Content Pane for a JPanel ? Pin
chdboy3-Feb-14 18:43
chdboy3-Feb-14 18:43 
Questionmultiplatform systems peer to peer LAN Mesengger Pin
Member 105671503-Feb-14 18:17
Member 105671503-Feb-14 18:17 
SuggestionRe: multiplatform systems peer to peer LAN Mesengger Pin
Richard MacCutchan3-Feb-14 21:57
mveRichard MacCutchan3-Feb-14 21:57 
GeneralRe: multiplatform systems peer to peer LAN Mesengger Pin
Member 105671505-Feb-14 17:13
Member 105671505-Feb-14 17:13 
GeneralRe: multiplatform systems peer to peer LAN Mesengger Pin
Richard MacCutchan5-Feb-14 22:25
mveRichard MacCutchan5-Feb-14 22:25 
GeneralRe: multiplatform systems peer to peer LAN Mesengger Pin
Member 105671509-Feb-14 17:09
Member 105671509-Feb-14 17:09 
GeneralRe: multiplatform systems peer to peer LAN Mesengger Pin
Member 1056715010-Feb-14 17:30
Member 1056715010-Feb-14 17:30 
QuestionJava Code for converting RGB image to Ycbcr image Pin
Member 105657221-Feb-14 20:30
Member 105657221-Feb-14 20:30 
I tired this code for converting RGB image to Ycbcr but it doesnt work this code also contain to covert RGB to Grayscale

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.image.*;
import javax.imageio.ImageIO;

public class Picture{
JFileChooser fileChooser = new JFileChooser();
final JFrame frame = new JFrame("Edit Image");
Container content;
static BufferedImage image;
BufferedImage image2;
JLabel imageLabel;

public Picture() {
//asks for image file as input
fileChooser.setDialogTitle("Choose an image file to begin:");
fileChooser.showOpenDialog(frame);

File selectedFile = fileChooser.getSelectedFile();
if (fileChooser.getSelectedFile() != null) {
try {
//reads File as image
image = ImageIO.read(selectedFile);
}
catch (IOException e) {
System.out.println("Invalid image file: " + selectedFile);
System.exit(0);
}
}
else {
System.out.println("No File Selected!");
}
}

public int width() {
//returns width of present image
int width = image.getWidth();
return width;
}

public int height() {
//returns height of present image
int height = image.getHeight();
return height;
}
/*
public void getImage() {
this.image = image2;
}
*/
public void saveImage() {
//saves current image as JPEG
fileChooser.setDialogTitle("Save this image?");
fileChooser.showSaveDialog(frame);
try {
//writes new file
ImageIO.write(this.image, "JPG", fileChooser.getSelectedFile());
}
catch (IOException f) {
System.out.println("Saving failed! Could not save image.");
}
}

public void show() {

//set frame title, set it visible, etc
content = frame.getContentPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);

//add the image to the frame
ImageIcon icon = new ImageIcon(image);
imageLabel = new JLabel(icon);
frame.setContentPane(imageLabel);

//add a menubar on the frame with a single option: saving the image
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu progName = new JMenu("Edit Image");
progName.setBackground(Color.RED);
menuBar.add(progName);
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
JMenu editMenu = new JMenu("Edit");
menuBar.add(editMenu);



ImageIcon exitIcon = new ImageIcon("app-exit.png");
JMenuItem exitAction = new JMenuItem("Exit", exitIcon);
progName.add(exitAction);
exitAction.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ImageIcon saveIcon = new ImageIcon("save-icon.png");
int askSave = JOptionPane.showConfirmDialog(null,"Save current image?", "", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, saveIcon);
if (askSave == JOptionPane.YES_OPTION) {
//opens save image method, then exits
saveImage();
System.exit(0);
}
else {
//exits without saving
System.exit(0);
}
}
});


ImageIcon newIcon = new ImageIcon("new-image.png");
JMenuItem newAction = new JMenuItem("Open Image", newIcon);
fileMenu.add(newAction);
newAction.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ImageIcon saveIcon = new ImageIcon("save-icon.png");
int askSave = JOptionPane.showConfirmDialog(null,"Save current image?", "", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, saveIcon);
if (askSave == JOptionPane.YES_OPTION) {
//opens save image method, then asks asks for new image file
saveImage();

Picture p = new Picture();
imageLabel.setIcon(new ImageIcon(image));
//resizes canvas to fit new image
frame.setSize(width(), height());
}
else {
//asks for new image file since user did not want to save original
Picture p = new Picture();
imageLabel.setIcon(new ImageIcon(image));
//resizes canvas to fit new image
frame.setSize(width(), height());
}
}
});

ImageIcon saveIcon = new ImageIcon("save-image.png");
JMenuItem saveAction = new JMenuItem("Save Image As...", saveIcon);
fileMenu.add(saveAction);
saveAction.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//opens save image method
saveImage();
}
});
ImageIcon gsIcon = new ImageIcon("grayscale-image.png");
JMenuItem grayScale = new JMenuItem("Grayscale", gsIcon);
editMenu.add(grayScale);
grayScale.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//grabs height and width of image, then grayscales it
grayscale(width(), height());
}
});
ImageIcon ycrbIcon = new ImageIcon("ycrcb-image.png");
JMenuItem ycrb = new JMenuItem("YCrCB", ycrbIcon);
editMenu.add(ycrb);
ycrb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//RGB
int height=height();
int width=width();

getRGB_YCC( width,height,image);


}
});

ImageIcon scaleIcon = new ImageIcon("scale-image.png");
JMenuItem scaleImg = new JMenuItem("Scale Image", scaleIcon);
editMenu.add(scaleImg);
scaleImg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//asks for height and width to create new image
ImageIcon widthIcon = new ImageIcon("LR-arrows.png");
String scaleWidth = (String)JOptionPane.showInputDialog(null,"What should the new width be?", "", JOptionPane.QUESTION_MESSAGE, widthIcon, null, null);
ImageIcon heightIcon = new ImageIcon("UD-arrows.png");
String scaleHeight = (String)JOptionPane.showInputDialog(null,"What should the new height be?", "", JOptionPane.QUESTION_MESSAGE, widthIcon, null, null);
//turns user input strings into doubles
double x = Double.parseDouble(scaleWidth);
double y = Double.parseDouble(scaleHeight);
//casts doubles as ints
int newWidth = (int)x;
int newHeight = (int)y;
//resizes frame to fit new image dimensions
frame.setSize(newWidth, newHeight);
//calls scale method to resize image using given dimensions
scale(newWidth, newHeight);
}
});
ImageIcon rotateIcon = new ImageIcon("rotate-image.png");
JMenuItem rotateImg = new JMenuItem("Rotate Image", rotateIcon);
editMenu.add(rotateImg);
rotateImg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

}
});

//paint the frame
frame.pack();
frame.repaint();
frame.setVisible(true);
}

// convert to grayscale
public void grayscale(int width, int height) {
// create a grayscale image with original dimensions
image2 = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);

// convert colored image to grayscale
ColorConvertOp grayScale = new ColorConvertOp(image.getColorModel().getColorSpace(),image2.getColorModel().getColorSpace(),null);
grayScale.filter(image,image2);
imageLabel.setIcon(new ImageIcon(image2));
//getImage();
image = image2;
}
// convert to ycrcb

/* public void rgbycbcr()
{
int r=255,g=255,b=255;
int h=height();
int w=width();
int total_pixels = (h * w);
Color[] colors = new Color[total_pixels];
int i = 0;

for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
colors[i] = new Color(image.getRGB(x, y));
i++;
}
}
for (i = 0; i < total_pixels; i++)
{
Color c = colors[i];
r = c.getRed();
g = c.getGreen();
b = c.getBlue();
}

//rgb2ycbcr(int r,int g,int b, int[] ycbcr)
//{
int [] ycbcr={255,255,255};
int y = (int)( 0.299 * r + 0.587 * g + 0.114 * b);
int cb = (int)(-0.16874 * r - 0.33126 * g + 0.50000 * b);
int cr = (int)( 0.50000 * r - 0.41869 * g - 0.08131 * b);

ycbcr[0] = y;
ycbcr[1] = cb;
ycbcr[2] = cr;
//}
}*/
public void getRGB_YCC(int width,int height,String inFileName) {
R=new int[height][width];G=new int[height][width];
B=new int[height][width];Y=new int[height][width];
Cb1=new int[height][width];Cr1=new int[height][width];
final int values[] = new int[width * height];
int r, g, b, Y_ch,Cb,Cr, y, x;

final PixelGrabber grabber = new PixelGrabber(image.getSource(), 0, 0,width,height, values, 0, width);

try {
if (grabber.grabPixels() != true) {
try {
throw new AWTException("Grabber returned false: " + grabber.getStatus());
} catch (final Exception e) {};
}
} catch (final InterruptedException e) {};
int index = 0;
for (y = 0; y < height; ++y) {
for (x = 0; x < width; ++x) {
r = values[index] >> 16 & 0xff;
g = values[index] >> 8 & 0xff;
b = values[index] & 0xff;

Y_ch= (int)(0.299 * r + 0.587 * g + 0.114 * b);
Cb= 128 + (int) (-0.16874 * r - 0.33126 * g + 0.5 * b);
Cr= 128 + (int)(0.5 * r - 0.41869 * g - 0.08131 * b);
R [y][x]=r;
G [y][x]=g;
B [y][x]=b;
Y [y][x]=Y_ch;
Cb1[y][x]=Cb;
Cr1[y][x]=Cr;
index++;
}
}
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
for( y=0;y
SuggestionRe: Java Code for converting RGB image to Ycbcr image Pin
Richard MacCutchan1-Feb-14 21:39
mveRichard MacCutchan1-Feb-14 21:39 
AnswerRe: Java Code for converting RGB image to Ycbcr image Pin
SOHAM_GANDHI3-Feb-14 5:25
SOHAM_GANDHI3-Feb-14 5:25 
QuestionJava Code for Wavelet Transform based algorithm for Image inpainting Pin
Member 105657221-Feb-14 18:59
Member 105657221-Feb-14 18:59 
QuestionImage inpainting Using Wavelet Transform in Java-Project Pin
Member 105657221-Feb-14 18:51
Member 105657221-Feb-14 18:51 
AnswerMessage Closed Pin
1-Feb-14 19:17
professionalPeter Leow1-Feb-14 19:17 
GeneralRe: Image inpainting Using Wavelet Transform in Java-Project Pin
Member 105657221-Feb-14 19:43
Member 105657221-Feb-14 19:43 
GeneralRe: Image inpainting Using Wavelet Transform in Java-Project Pin
Peter Leow1-Feb-14 20:19
professionalPeter Leow1-Feb-14 20:19 
GeneralRe: Image inpainting Using Wavelet Transform in Java-Project Pin
Member 105657221-Feb-14 20:33
Member 105657221-Feb-14 20:33 
QuestionRe: psd to jpg or png conversion Pin
Shahid abbasi31-Jan-14 1:33
Shahid abbasi31-Jan-14 1:33 
SuggestionRe: psd to jpg or png conversion Pin
Richard MacCutchan31-Jan-14 2:53
mveRichard MacCutchan31-Jan-14 2:53 
Questionjava project Pin
sairam209329-Jan-14 16:47
sairam209329-Jan-14 16:47 
QuestionRe: java project Pin
thatraja29-Jan-14 19:53
professionalthatraja29-Jan-14 19:53 
Questionjava program for finding network printer ink status dynamically Pin
Member 1054008129-Jan-14 0:56
Member 1054008129-Jan-14 0:56 
AnswerRe: java program for finding network printer ink status dynamically Pin
Richard MacCutchan29-Jan-14 2:46
mveRichard MacCutchan29-Jan-14 2:46 
QuestionScriptable Java Applications Pin
Otto Grunf28-Jan-14 8:07
Otto Grunf28-Jan-14 8:07 
AnswerRe: Scriptable Java Applications Pin
jschell28-Jan-14 10:32
jschell28-Jan-14 10:32 
GeneralRe: Scriptable Java Applications Pin
Otto Grunf29-Jan-14 8:10
Otto Grunf29-Jan-14 8:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.