📄 imagemain.java
字号:
package testconditon.imageload;
// imageMain.java
// This main() method illustrates how to perform image operations:
// - read an image from an input file. The user is asked to type a
// file name. The file can be in gif, jpeg or bmp format.
// - write an image to a bmp output file. The user is asked to type the
// file name.
// - modify the image
// apply a green filter to the top half,
// apply a fading green filter across the whole image,
// add vertical black bars to the image,
// convert to a grayscale image
// The user selects which operations to apply.
//
// Within the Java code, a colour image is represented using a
// three-dimensional array.
// The first index selects one row of the image.
// The second index selects on column of the image.
// The third index selects a colour (RED, GREEN, or BLUE).
// For example, "testImage[3][5][RED]" is an integer value in the range
// 0 to 255. A 0 indicates that there is no red colour at row 3, column 5
// in testImage. A 255 indicates that there is maximal red colour at
// this location.
// If RED, GREEN, and BLUE are all 0, the image appears black. If they
// are all 255, the image appears white.
//
import java.util.*;
import java.io.*;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class imageMain {
// Give names to the constants used for the red, green and blue
// values in an image.
// The use of final variables is discussed on page 10-11 of the
// Main textbook used in CISC124.
// Red, green and blue are 8 bit quantities, each with a value
// between 0 and 255. In a file, each pixel occupies a 32 bit word.
// Since red, green and blue need a total of 24 bits, this leaves
// 8 extra bits. These extra bits are called "offset". You never need
// to change the value of the offset bits.
final static int RED = 0;
final static int GREEN = 1;
final static int BLUE = 2;
final static int OFFSET = 3; // ignore offset; use only red, green, blue
///define the variable
public int XX;
public int YY;
/*
* flag 声明
* CONST_INTERFACE = 8 CONST_FLUID = 9, CONST_EMPTY = 0, CONST_WALL = 1, CONST_NONE = 2,
*/
public static final int CONST_INTERFACE = 8;
public static final int CONST_FLUID = 9;
public static final int CONST_EMPTY = 0;
public static final int CONST_WALL = 1;
public static final int CONST_NONE = 2;
/*
* 其他常量声明
*/
public static final int SquareLow = 31;
public static final int SquareUp = 69;//正方形在40X40的区�?
public static final double r0=1;//密度
public static final double MassFluid=1;//质量
public static final double MassInterface=0.5; //界面质量
public static final double MassAero=0;//空气质量
/*
*变量声明
*/
public int[][] flag;
public int[][][] normal_dirction;
public double[][] u;
public double[][] v;
public double[][] rho;
public double[][] mass;
public double[][] UU;
// The console is used to receive input from the user. In this program,
// the user is asked to type file names.
static public BufferedReader console = new BufferedReader(
new InputStreamReader(System.in));
//
// ****************************** main **********************************
// The main method reads an input image file, and allows the user to
// select from a set of modifications that can be applied to the image.
//
public void imageimport() {
// This program is supposed to be run without any arguments.
// If the user supplies arguments, issue an explanatory message.
// if (args.length!=0) {
// System.out.println("Usage: java imageProgram");
// System.exit(1); // exit. The exit code 1 indicates failure
// }
// Ask the user for a file name and read the contents of this
// file into "testImage".
// After method "loadImage" completes, the size of the image
// is available as follows:
// - the number of rows is testImage.length
// - the number of columns is testImage[0].length
int[][][] testImage = imageIO.loadImage();
final int MAXROWS = testImage.length;
final int MAXCOLS = testImage[0].length;
System.out.println("max rows is "+MAXROWS);
System.out.println("max cols is "+MAXCOLS);
XX=MAXROWS;
YY=MAXCOLS;
flag = new int[XX+1][YY+1];
normal_dirction = new int [XX+1][YY+1][2];
u = new double[XX+1][YY+1];
v = new double[XX+1][YY+1];
rho = new double[XX+1][YY+1];
mass = new double[XX+1][YY+1];
UU = new double [XX+1][YY+1];
// Repeatedly offer the user a list of operations to choose from
// Sample operations are provided, to illustrate how an image
// can be updated.
//
while (true) {
System.out.println("");
System.out.println("What operation do you want to perform? ");
System.out.println(" 1 read initial condition from picture");
System.out.println(" 2 out put data file");
System.out.println(" 3 quit");
String reply = null;
try{
reply = console.readLine();
}
catch(IOException e) {
System.out.println(e);
System.exit(1);
}
if (reply.startsWith("1")) {
for (int row=0; row<MAXROWS; row++) {
for (int col=0; col<MAXCOLS; col++) {
u[row][col]=0;
v[row][col]=0;
rho[row][col]=r0;
UU[row][col]=col;
System.out.println(row+" "+col+"\n");
//增加流体�?
if( testImage[row][col][GREEN] == 255){
flag[row][col]=CONST_FLUID;
mass[row][col]=MassFluid;
System.out.println(row+" "+col+"is green and it is fluid"+"\n");
}
//增加interface
else if (testImage[row][col][RED] == 255){
flag[row][col]=CONST_INTERFACE;
mass[row][col]=MassInterface;
System.out.println(row+" "+col+"is red and it is interface"+"\n");
}
//增加wall
else if(testImage[row][col][BLUE] == 255){
flag[row][col]= CONST_WALL;
if(row==0){
normal_dirction[row][col][0]=1;
normal_dirction[row][col][1]=0;
}
else if(row==XX){
normal_dirction[row][col][0]=-1;
normal_dirction[row][col][1]=0;
}
else if(col==0){
normal_dirction[row][col][0]=0;
normal_dirction[row][col][1]=1;
}
else if(col==YY){
normal_dirction[row][col][0]=0;
normal_dirction[row][col][1]=-1;
}
System.out.println(row+" "+col+"is blue and it is wall"+"\n");
}
//添加空的区域
else {
flag[row][col]=CONST_EMPTY ;
mass[row][col]=MassAero;
System.out.println(row+" "+col+"\n");
}
} // for col
} // for row2
System.out.println("pic has been read");
} // if reply is 1
if (reply.startsWith("2")) {
int x,y;
int tXX=XX+1;
int tYY=YY+1;
PrintWriter outFile=null;
try {
outFile = new PrintWriter(new FileWriter("initfile"));
outFile.println("version zw");
outFile.println(tXX+" "+tYY);
for(x=0; x<=XX; x++) {
for (y=0; y<=YY; y++) {
//分情况输�?
//interface
if(flag[x][y]==CONST_INTERFACE){
outFile.print(flag[x][y]+" "+u[x][y] +" "+v[x][y] +" "+rho[x][y] +" "+mass[x][y]+" "+UU[x][y]);
}
//fluid
else if(flag[x][y]==CONST_FLUID){
outFile.print(flag[x][y]+" "+u[x][y] +" "+v[x][y] +" "+mass[x][y]+" "+UU[x][y]);
}
//empty
else if(flag[x][y]==CONST_EMPTY){
outFile.print(flag[x][y]+" "+UU[x][y]);
}
//wall
else if(flag[x][y]==CONST_WALL){
outFile.print(flag[x][y]+" "+ normal_dirction[x][y][0] +" "+ normal_dirction[x][y][1]+" "+UU[x][y]);
}
//NONE
else if (flag[x][y]==CONST_NONE){
outFile.print(flag[x][y]+" "+UU[x][y]);
}
else{
}
outFile.println();
}
}
}catch (IOException e) {
e.printStackTrace();
} finally {
outFile.close();
}
}
else if (reply.startsWith("3")) {
try{
console.close();
}
catch(IOException e) {
System.out.println(e);
System.exit(1);
}
System.exit(1);
} // if reply is 6
// The user's reply was not recognized as a command.
//else System.out.println("Unrecognized command. Please try again.");
} // end of "while true"
} // end of main()
} // end of class imageMain
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -