📄 imagemain.java
字号:
package imageFilter;
import java.awt.image.* ;
import java.io.* ;
import javax.imageio.* ;
public class ImageMain {
private static short[][][] getArrayFromFile( String fileName ) throws IOException {
File file = new File(fileName) ;
BufferedImage buffer = ImageIO.read( file ) ;
if( buffer == null ) return null ;
int height = buffer.getHeight() ;
int width = buffer.getWidth() ;
short[][][] result = new short[width][height][3] ;
for( int x = 0 ; x < width ; ++x ) {
for( int y = 0 ; y < height ; ++y ) {
// Note that minX == minY == 0 for bufferedImage.
int pixel = buffer.getRGB(x, y) ;
int red = (pixel & 0x00FF0000) >>> 16 ;
int green = (pixel & 0x0000FF00) >>> 8 ;
int blue = (pixel & 0x000000FF) ;
result[x][y][Filter.RED] = (short) red ;
result[x][y][Filter.GREEN] = (short) green ;
result[x][y][Filter.BLUE] = (short) blue ; } }
return result ;
}
private static void writeArrayToFile( String fileName, short[][][] raster )
throws IOException {
int width = raster.length ;
int height = width==0 ? 0 : raster[0].length ;
BufferedImage buffer = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB ) ;
for( int x = 0 ; x < width ; ++x ) {
for( int y = 0 ; y < height ; ++y ) {
int red = raster[x][y][Filter.RED] & 0x00FF ;
int green = raster[x][y][Filter.GREEN] & 0x00FF ;
int blue = raster[x][y][Filter.BLUE] & 0x00FF ;
int pixel = 0xFF000000 | (red << 16) | (green << 8) | blue ;
buffer.setRGB(x, y, pixel) ; } }
File file = new File( fileName ) ;
ImageIO.write( buffer, "jpeg", file ) ;
}
static public void main( String[] args ) {
final String usage = "Usage: java imageFilter.ImageMain filterNumber inFile.jpg outFile.jpg" ;
if( args.length != 3 ) {
System.err.println( usage ) ;
System.exit(1) ;
}
Filter[] filters = new Filter[]{ new RotateClockwiseFilter(), new GreyFilter(), new MyFilter()} ;
int filterIndex ;
try {
filterIndex = Integer.parseInt( args[0] ) ;
} catch( NumberFormatException e ) {
System.err.println( usage ) ;
System.err.println( "filterNumber must be an integer" ) ;
System.exit(1) ;
filterIndex = 0 ; }
if( filterIndex < 0 || filterIndex >= filters.length ) {
System.err.println( usage ) ;
System.err.println( "filterNumber must be an integer between 0 and "+(filters.length-1)+" inclusive." ) ;
System.exit(1) ; }
Filter filter = filters[ filterIndex ] ;
String inFileName = args[1] ;
String outFileName = args[2] ;
short[][][] inRaster ;
try {
inRaster = getArrayFromFile( inFileName ) ;
} catch (IOException e) {
e.printStackTrace();
System.exit(1) ;
inRaster = null ;
}
int width = inRaster.length ;
int height = width==0 ? 0 : inRaster[0].length ;
short[][][] outRaster = filter.process( width, height, inRaster ) ;
try {
writeArrayToFile( outFileName, outRaster ) ;
} catch( IOException e ) {
e.printStackTrace();
System.exit(1) ;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -