Instructions
Objective
Write a program to create channel selection system in java.
Requirements and Specifications
Source Code
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import java.awt.image.BufferedImage;
import java.awt.Color;
public class ChannelSelection extends Application {
@FXML
private TextField dimensionsTxt;
@FXML
private ImageView imgView;
@FXML
private MenuItem openMenuBttn;
@FXML
void initialize() {
assert dimensionsTxt != null : "fx:id=\"dimensionsTxt\" was not injected: check your FXML file 'layout.fxml'.";
assert imgView != null : "fx:id=\"imgView\" was not injected: check your FXML file 'layout.fxml'.";
assert openMenuBttn != null : "fx:id=\"openMenuBttn\" was not injected: check your FXML file 'layout.fxml'.";
}
public void start(Stage primaryStage) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("layout_channelselection.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root, 510, 420);
primaryStage.setTitle("Channel Selection");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String args[]) {
launch(args);
}
@FXML
void loadImage(ActionEvent event) throws IOException {
// pick image
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
//creating the image object
//InputStream stream = new FileInputStream("garden.jpg");
BufferedImage image = ImageIO.read(new File(selectedFile.getAbsolutePath()));
// Get dimensions
int width = image.getWidth();
int height = image.getHeight();
WritableImage wr = new WritableImage(width, height);
PixelWriter pw = wr.getPixelWriter();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
// Get argb
int pixel = image.getRGB(x,y);
Color color = new Color(pixel);
//int alpha = (pixel >> 24) & 0xff;
/*int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel >> 1) & 0xff;*/
int red = color.getRed();
int blue = color.getBlue();
int green = color.getGreen();
// Now, pick brightest
// Check if color is almost white
if(red >= 200 && green >= 200 && blue >= 200) { // nearly white
red = 0;
green = 0;
blue = 255;
} else if (blue <= 50 && red <= 50 && green <= 50) {
red = 0;
blue = 0;
green = 0;
} else if (blue >= red && blue >= green) {
green = 0;
red = 0;
} else if (red >= blue && red >= green) {
blue = 0;
green = 0;
} else if(green >= red && green >= blue) {
red = 0;
blue = 0;
}
// Now, convert to argb
//int argb = alpha << 24 + newcol[0] << 16 + newcol[1] << 8 + newcol[2];
int argb = (255 << 24) | (red << 16) | (green << 8) | (blue << 1);
pw.setArgb(x, y, argb);
}
}
//Image image = new Image(stream);
imgView.setImage(wr);
//Setting the image view parameters
imgView.setPreserveRatio(true);
// Display dimensions
dimensionsTxt.setText(String.format("Width = %d, Height = %d", width, height));
}
}
}
Related Samples
Discover our free programming assignment samples, crafted to help you grasp difficult concepts and improve your coding skills. Our examples cover a wide range of topics, providing valuable insights and solutions. Check them out today!
Programming
Programming
Programming
Programming
Programming
Programming
Programming
Programming
Programming
Programming
Programming
Programming
Programming
Programming
Programming
Programming
Programming
Programming
Programming
Programming