java swing image processing 您所在的位置:网站首页 newimage/wlast2.gif java swing image processing

java swing image processing

2023-01-22 03:30| 来源: 网络整理| 查看: 265

[Function description: 1. Click the mouse: select the cropped area 2. Double-click: select the cropped area, save the cropped area as a file, and save the coordinates of the center point of the area at the same time 3. Open the picture: open the picture from a certain folder and display it, At the same time put the

I don't know which netizen got it from. [1 Implementation of JButton image and text buttons JButton btn1 = new JButton("Open", new ImageIcon(ImageView.class.getResource("10.png"))) ; btn1.setHorizontalText

import java.awt.BorderLayout;

import java.awt.Image;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.image.BufferedImage;

import java.awt.image.ColorModel;

import java.awt.image.MemoryImageSource;

import java.awt.image.PixelGrabber;

import java.io.File;

import java.io.IOException;

import java.util.LinkedList;

import javax.imageio.ImageIO;

import javax.swing.ImageIcon;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import javax.swing.JScrollPane;

import javax.swing.filechooser.FileFilter;

public class MyShowImage extends JFrame {

// Save the pixel matrix of the current operation

private int currentPixArray[] = null;

// path to the image

private String fileString = null;

// label to display the image

private JLabel imageLabel = null;

// loaded image

private BufferedImage newImage;

// image height and width

private int h, w;

// Save history operation image matrix

private LinkedList imageStack = new LinkedList();

private LinkedList tempImageStack = new LinkedList();

public MyShowImage(String title) {

super(title);

this. setSize(800, 600);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// create menu

JMenuBar jb = new JMenuBar();

JMenu fileMenu = new JMenu("File");

jb. add(fileMenu);

JMenuItem openImageMenuItem = new JMenuItem("Open image");

fileMenu.add(openImageMenuItem);

openImageMenuItem. addActionListener(new OpenListener());

JMenuItem exitMenu = new JMenuItem("Exit");

fileMenu.add(exitMenu);

exitMenu. addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

System. exit(0);

}

});

JMenu operateMenu = new JMenu("Image processing");

jb.add(operateMenu);

JMenuItem RGBtoGrayMenuItem = new JMenuItem("Grayscale image conversion");

operateMenu.add(RGBtoGrayMenuItem);

RGBtoGrayMenuItem.addActionListener(new RGBtoGrayActionListener());

JMenuItem balanceMenuItem = new JMenuItem("Balanced");

operateMenu.add(balanceMenuItem);

balanceMenuItem. addActionListener(new BalanceActionListener());

JMenu frontAndBackMenu = new JMenu("History Operation");

jb.add(frontAndBackMenu);

JMenuItem backMenuItem = new JMenuItem("Back");

frontAndBackMenu.add(backMenuItem);

backMenuItem. addActionListener(new BackActionListener());

JMenuItem frontMenuItem = new JMenuItem("forward");

frontAndBackMenu.add(frontMenuItem);

frontMenuItem. addActionListener(new FrontActionListener());

this.setJMenuBar(jb);

imageLabel = new JLabel("");

JScrollPane pane = new JScrollPane(imageLabel);

this.add(pane, BorderLayout.CENTER);

this. setVisible(true);

}

private class OpenListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

JFileChooser jc = new JFileChooser();

jc.setFileFilter(new FileFilter() {

public boolean accept(File f) { // Set the suffix of the available file

if (f.getName().endsWith(".jpg") || f.isDirectory()|| f.getName().endsWith(".gif") || f.getName().endsWith(".bmp ")) {

return true;

}

return false;

}

public String getDescription() {

return "Picture(*.jpg,*.gif,*bmp)";

}

});

int returnValue = jc. showOpenDialog(null);

if (returnValue == JFileChooser. APPPROVE_OPTION) {

File selectedFile = jc. getSelectedFile();

if (selectedFile != null) {

fileString = selectedFile. getAbsolutePath();

try {

newImage = ImageIO. read(new File(fileString));

w = newImage. getWidth();

h = newImage. getHeight();

currentPixArray = getPixArray(newImage, w, h);

imageStack. clear();

tempImageStack. clear();

imageStack. addLast(currentPixArray);

imageLabel.setIcon(new ImageIcon(newImage));

} catch (IOException ex) {

System.out.println(ex);

}

}

}

MyShowImage. this. repaint();

// MyShowImage. this. pack();

}

}

// menu listener ///

private class RGBtoGrayActionListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

int[] resultArray = RGBtoGray(currentPixArray);

imageStack. addLast(resultArray);

currentPixArray = resultArray;

showImage(resultArray);

tempImageStack. clear();

}

}

private class BalanceActionListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

int[] resultArray = balance(currentPixArray);

imageStack. addLast(resultArray);

currentPixArray = resultArray;

showImage(resultArray);

tempImageStack. clear();

}

}

private class BackActionListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

if (imageStack. size()

tempImageStack. addLast(imageStack. removeLast());

currentPixArray = imageStack. getLast();

showImage(currentPixArray);

}

}

}

private class FrontActionListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

if (tempImageStack. size() < 1) {

JOptionPane.showMessageDialog(null, "The processing of this picture has no forward history operation", "Prompt",

JOptionPane. INFORMATION_MESSAGE);

} else {

currentPixArray = tempImageStack. removeFirst();

imageStack. addLast(currentPixArray);

showImage(currentPixArray);

}

}

}

// Get image pixel matrix/

private int[] getPixArray(Image im, int w, int h) {

int[] pix = new int[w * h];

PixelGrabber pg = null;

try {

pg = new PixelGrabber(im, 0, 0, w, h, pix, 0, w);

if (pg. grabPixels() != true)

try {

throw new java.awt.AWTException("pg error" + pg.status());

} catch (Exception eq) {

eq. printStackTrace();

}

} catch (Exception ex) {

ex. printStackTrace();

}

return pix;

}

// display image///

private void showImage(int[] srcPixArray) {

Image pic = createImage(new MemoryImageSource(w, h, srcPixArray, 0, w));

ImageIcon ic = new ImageIcon(pic);

imageLabel.setIcon(ic);

imageLabel. repaint();

}

// grayscale conversion///

private int[] RGBtoGray(int[] ImageSource) {

int[] grayArray = new int[h * w];

ColorModel colorModel = ColorModel. getRGBdefault();

int i, j, k, r, g, b;

for (i = 0; i < h; i++) {

for (j = 0; j < w; j++) {

k = i * w + j;

r = colorModel.getRed(ImageSource[k]);

g = colorModel. getGreen(ImageSource[k]);

b = colorModel. getBlue(ImageSource[k]);

int gray = (int) (r * 0.3 + g * 0.59 + b * 0.11);

r = g = b = gray;

grayArray[i * w + j] = (255

for (int j = 0; j < w; j++) {

int gray = srcPixArray[i * w + j] & 0xff;

histogram[grey]++;

}

}

double a = (double) 255 / (w * h);

double[] c = new double[256];

c[0] = (a * histogram[0]);

for (int i = 1; i < 256; i++) {

c[i] = c[i - 1] + (int) (a * histogram[i]);

}

for (int i = 0; i < h; i++) {

for (int j = 0; j < w; j++) {

int gray = srcPixArray[i * w + j] & 0x0000ff;

int hist = (int) c[grey];

dinPixArray[i * w + j] = 255



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有