Through a series of knowledge learning and improvement, I made a simple image processing program and used some basic methods. It has a certain auxiliary effect on the familiar code of beginners.
1. Programming
1. Introduce the window class and determine the main function as the procedure entry.
2. Add a monitor and set multiple buttons to achieve multiple functions.
3. Read the photos through Imageio.read (File).
4. Use the bufferedImage method to quickly draw pictures.
2. Method introduction
First determine a window and set the function we want on the window:
public class imageui extends jframe {
// Create the Listener object while creating IU objects
Imagelistener Listener = New ImageListener ();
Public Static Void Main (String ARGS []) {) {
ImageUI IU = New Imageui ();
IU.initui ();
}
public void initui () {{)
settitle ("Graphic Image Processing");
setsize (1000,1000);
setdefaultCloseoperation (jframe.exit_on_close);
setlocationRelativeto (null); // Display in the center
// Add layout
Flowlayout flow = New FlowLayout ();
setLayout (Flow);
addjbutton ();
setvisible (true);
// Get the brush from the window
Graphics g = this.getgraphics ();
// Pass the brush on the window to the listener object
listner.g = g;
listner.iu = this;
}
When the set button is set, we can adopt a cycle method for the simplicity of the program:
public void addjbutton () {
String [] btnstrs = {
"Original picture", "mosaic", "anti -film", "gray", "dual value", "nostalgic", "oil painting", "outline extract",
};
for (int i = 0; I <bTNSTRS.Length; I ++) {
// Add the circular structure here to implement the display of the button
String btnstr = BTNSTRS [i];
Jbutton btn = new jbutton (btnstr);
add (btn);
BTN.AdDactionListener (Listener);
}
}
We can find that we have created a Listener object. It is the key to we add a monitor to the button next. Then we add the button to the button and rewrite the method in the listener:
Import java.awt.graphics;
Import java.awt.event.actions;
Import java.awt.event.actionListener;
Import java.awt.image.buffRedimage;
Public Class Images Implements ActionListener {
// Create an empty pointer to receive the object obtained on the window
Graphics g;
ImageUi IU;
// Create the IS object here to obtain the components on the Listener object, such as the brush (the brush here is the brush obtained on the ImageUI)
ImageSolve is = New ImageSolve ();
// Create a bufferedInage array to save the drawing photos
BufferedImage buffinglist [] = new bufferedimage [200];
int buffingsize = 0;
int [] [] imagarr = {};
public imagener () {
// Construction method, pass the window to ImageSolve
is.IU = IU;
imagarr = is.imagearr ("C: \\ Users \\ 86177 \\ pictureres \\ saved pictures \\ unTitled.png");
}
Public Void ActionPerFormed (ActionEvent E) {{
String btnstr = e.getActionCommand ();
if (btnstr.equals ("Original Picture") {{
BufferedImage I = is.imageSolve01 (imagarr, g);
// Save the pictures you finish in each time
BuffingList [buffingsize] = i;
Buffingsize ++;
}
By the different contents on the comparison button, click different buttons to call different image processing methods. Here we only introduce a method of judging buttons, and other methods are easy to get. It should be noted here that we need to use brushes in different image processing methods. Here we can call it through the Listener object. The IU interface is the same.
In the constructive method, our purpose is to initialize the image and pass the IU parameter to the image processing method, because I define the initialization of the photo in the back:
public int
File file = new file (path);
BufferedImage buffing = null;
try {
buffing = Imageio.read (File);
} Catch (IOEXception E) {{
e.printstacktrace ();
}
// Get the height width of the photo
int w = buffing.getWidth ();
int h = buffing.getheight ();
// Storage of the position and pixel color ratio of each pixel point to the two -dimensional array
int [] [] imagarr = new int [w] [h];
for (int i = 0; I <IMAGARR.Length; I ++) {
for (int j = 0; j <imagarr.Length; j ++) {
imagarr [i] [j] = buffing.getRGB (i, j);
}
}
Return Imagarr;
}
So the photo has been determined. What we have to do next is to complete different processing methods. It should be noted that in order to ensure the efficiency of the photo processing and achieve the purpose of re -painting, I use the bufferedImage method and the repaint Methods to save the effect of each completed picture processing, we must also define a BufferedImage array. List various methods here:
1. Original picture
public BufferedImage imagesolve01(int[][]imagarr,Graphics g){
//
BufferedImage img=new BufferedImage(imagarr.length, imagarr[0].length, BufferedImage.TYPE_INT_ARGB);
for(int i = 0; i < imagarr.length; i++){
for(int j = 0; j < imagarr[i].length; j++){
int rgb = imagarr[i][j];
img.setRGB (i,j,rgb);
}
}
g.drawImage(img,200,100,null);
return img;
}
2. mosaic
Public bufferedImage Imagsolve02 (int [] [] [] imager, graphics g) {
// This is a method that draws the picture into the buffer area first.
BufferedImage IMG = New BufferedImage (Imagarr.Length, Imagrr [0] .Length, buffredimage.type_int_argb)
for (int i = 0; I <= Imagarr.Length-10; i = i+10) {
for (int j = 0; j <= Imagarr.Length-10; j = J+10) {
int RGB = Imagarr [i] [j];
// Here
Graphics bg = img.getgraphics ();
Color color = new color (RGB);
bg.setcolor (color);
bg.fillrect (i, j, 10, 10);
}
}
G.DrawImage (img, 200,100, null);
Return img;
}
3. Anti -film
Public bufferedImage ImageSolve03 (int [] [] [] imager, graphics g) {
// This is the second method of drawing the buffer picture. The method of using IMG's SetRGB is used to draw the photo into the buffer first, and then draw the picture
BufferedImage IMG = New BufferedImage (Imagarr.Length, Imagrr [0] .Length, buffredimage.type_int_argb)
for (int i = 0; I <IMAGARR.Length; I ++) {
for (int j = 0; j <imagarr.Length; j ++) {
int RGB = Imagarr [i] [j];
Color color = new color (RGB);
int Red = 255-COLOR.Getred ();
int Green = 255-color.getgreen ();
int blue = 255-color.getblue ();
Color ncolor = new color (red, geen, blue);
img.setRGB (i, J, NCOLOR.GETRGB ());
}
}
G.DrawImage (img, 200,100, null);
Return img;
}
4. Nostalgic
public BufferedImage imagesolve04(int[][]imagarr,Graphics g){
int nr;
int ng;
int nb;
BufferedImage img=new BufferedImage(imagarr.length, imagarr[0].length, BufferedImage.TYPE_INT_ARGB);
for(int i=0;i<imagarr.length;i++){
for(int j=0;j<imagarr.length;j++){
int rgb=imagarr[i][j];
Color color=new Color(rgb);
double red=0.393*color.getRed()+0.769*color.getGreen()+0.189*color.getBlue();
nr=(int)red;
if (nr>255) nr=255;
double green=0.349*color.getRed()+0.686*color.getGreen()+0.168*color.getBlue();
ng=(int)green;
if (ng>255) ng=255;
double blue=0.272*color.getRed()+0.534*color.getGreen()+0.131*color.getBlue();
nb=(int)blue;
if (nb>255) nb=255;
Color ncolor=new Color(nr,ng,nb);
img.setRGB(i,j,ncolor.getRGB());
}
}
g.drawImage(img,200,100,null);
return img;
}
5. Gray treatment
public BufferedImage imagesolve05(int[][]imagarr,Graphics g){
BufferedImage img=new BufferedImage(imagarr.length, imagarr[0].length, BufferedImage.TYPE_INT_ARGB);
for(int i=0;i<imagarr.length;i++){
for(int j=0;j<imagarr.length;j++){
int rgb=imagarr[i][j];
Color color=new Color(rgb);
int red=color.getRed();
int green=color.getGreen();
int blue=color.getBlue();
int gray=(red+green+blue)/3;
Color ncolor=new Color(gray,gray,gray);
img.setRGB(i, j, ncolor.getRGB());
}
}
g.drawImage(img,200,100,null);
return img;
}
6. Two -value
public BufferedImage imagesolve06(int[][]imagarr,Graphics g){
BufferedImage img=new BufferedImage(imagarr.length, imagarr[0].length, BufferedImage.TYPE_INT_ARGB);
for(int i=0;i<imagarr.length;i++){
for(int j=0;j<imagarr.length;j++){
Graphics bg=img.getGraphics();
int rgb=imagarr[i][j];
Color color=new Color(rgb);
int red=color.getRed();
int green=color.getGreen();
int blue=color.getBlue();
int gray=(red+green+blue)/3;
if(gray<=165){
bg.setColor(Color.white);
}
else{
bg.setColor(Color.BLACK);
}
bg.fillRect(i, j, 1, 1);
}
}
g.drawImage(img,200,100,null);
return img;
}
7. Outline extract
public BufferedImage imagesolve07(int[][]imagarr,Graphics g){
BufferedImage img=new BufferedImage(imagarr.length, imagarr[0].length, BufferedImage.TYPE_INT_ARGB);
for(int i=0;i<imagarr.length-2;i++){
for(int j=0;j<imagarr.length-2;j++){
Graphics ng=img.getGraphics();
int rgb=imagarr[i][j];
Color color=new Color(rgb);
int red=color.getRed();
int blue=color.getBlue();
int green=color.getGreen();
int gray=(red+blue+green)/3;
int nrgb=imagarr[i+2][j+2];
Color ncolor=new Color(nrgb);
int nred=ncolor.getRed();
int nblue=ncolor.getBlue();
int ngreen=ncolor.getGreen();
int ngray=(nred+nblue+ngreen)/3;
if(Math.abs(gray-ngray)<15){
ng.setColor(color.black);
}
else{
ng.setColor(color.white);
}
ng.fillRect(i, j, 1, 1);
}
}
g.drawImage(img,200,100,null);
return img;
}
Finally, it is to re -paint what we want to do. Pick the saved pictures one by one.
public void paint(Graphics g){
super.paint(g);
for(int i=0;i<listener.BuffingSize;i++){
BufferedImage buffimg = listener.BuffingList[i];
g.drawImage (buffimg, 200,100 , null);
}
}
So the above is our simple image processing program. There are shortcomings and hope to add, hoping to help beginners.