コードコピーは次のとおりです。
/**
*
*/
パッケージcom.b510;
java.awt.rectangleをインポートします。
java.awt.image.bufferedimageをインポートします。
java.io.fileをインポートします。
java.io.fileinputStreamをインポートします。
java.io.ioexceptionをインポートします。
Import Java.util.date;
java.util.iteratorをインポートします。
javax.imageio.imageioをインポートします。
javax.imageio.imagereadparamをインポートします。
javax.imageio.imagereaderをインポートします。
javax.imageio.stream.imageinputStreamをインポートします。
/**
* @date 2012-11-26
* @author xhw
*
*/
パブリッククラスのイメージカット{
/**
*ソースイメージパス名は次のとおりです。C:/1.jpg
*/
プライベート文字列srcpath = "e:/poool.jpg";
/**
*クリッピング画像ストレージパスの名前:c:/2.jpg
*/
private string subpath = "e:/pool_end";
/**
* JPG画像形式
*/
private static final string image_form_of_jpg = "jpg";
/**
* PNG画像形式
*/
private static final string image_form_of_png = "png";
/**
* Xせん断点の座標
*/
プライベートint x;
/**
*カットポイントY座標
*/
プライベートインストY;
/**
*せん断点幅
*/
プライベートインクト幅;
/**
*せん断点の高さ
*/
プライベートINT高さ;
public imagecut(){
}
Public ImageCut(int x、int y、int width、int height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public static void main(string [] args)スロー例外{
ImageCut ImageCut = new ImageCut(134、0、366、366);
imagecut.cut(imagecut.getsrcpath()、imagecut.getsubpath());
}
/**
*指定された形式をデコードできると主張する、現在登録されているすべてのイメージャリーダーを含むIteratorを返します。
*パラメーター:FormatName-「JPEG」または「TIFF」など)が含まれています。
*
* @param postfix
*ファイルサフィックス名
* @戻る
*/
public Iterator <ImageReader> getImagEReadersByFormatName(String PostFix){
switch(postfix){
ケースimage_form_of_jpg:
return imageio.getimagereadersbyformatname(image_form_of_jpg);
ケースimage_form_of_png:
return imageio.getimagereadersbyformatname(image_form_of_png);
デフォルト:
return imageio.getimagereadersbyformatname(image_form_of_jpg);
}
}
/**
*画像をトリミングし、トリミング後に新しい画像を保存します。
* @Param SRCPATHソースイメージパス
* @param Subpath画像ストレージパス
* @throws ioexception
*/
パブリックボイドカット(String SrcPath、String Subpath)はioExceptionをスローします{
fileInputStream is = null;
ImageInputStream iis = null;
試す {
//画像ファイルを読み取ります
IS = new FileInputStream(srcpath);
//ファイルの接尾辞名を取得します
文字列postfix = getPostFix(srcpath);
system.out.println( "画像形式は:" + postfix);
/*
*指定された形式をデコードできると主張する、現在登録されているすべてのイメージャリーダーを含むIteratorを返します。
*パラメーター:FormatName-「JPEG」または「TIFF」など)が含まれています。
*/
iterator <ImagEReader> it = getImagEReadersByFormatName(POSTFIX);
ImagEReader reader = it.next();
//画像ストリームを取得します
iis = imageio.createimageinputStream(is);
/*
* <p> iis:source.true:forwardのみを検索します</p>。
*この設定は、入力ソースに含まれる画像が順番に読み取られることを意味します。これにより、読者は以前に読み取られた画像を含むデータの入力部分をキャッシュしないようにすることができます。
*/
reader.setInput(iis、true);
/*
* <p>入力でJava画像I/Oからデコードする方法を指定するために使用されるクラス。
*フレームのコンテキストでストリーミング画像または画像のグループを変換します。特定の画像形式のプラグインは、そのimagreaderから実装されます
* getDefaulTreadParamメソッドは、ImagEReadParamのインスタンスを返します。
*/
ImagEReadParam pAram = reader.getDefaulTreadParam();
/*
*画像収穫面積。長方形は、長方形オブジェクトを介して座標空間内の領域を指定します
*の左上頂点の座標(x、y)、幅、高さは、この領域を定義できます。
*/
長方形rect = new Rectangle(x、y、幅、高さ);
//緩衝液を提供して、ピクセルデータを解読するためのターゲットとして使用します。
param.setsourceregion(rect);
/*
*提供されたImagEReadParamを使用して、ImageIndexをインデックス作成することによって指定されたオブジェクトを読み取り、完全なものとして使用します
* bufferedimage戻り。
*/
bufferedimage bi = reader.read(0、param);
//新しい画像を保存します
Imageio.write(bi、postfix、new file(subpath + "_" + new date()。gettime() + "。" + postfix));
} ついに {
if(is!= null)
is.close();
if(iis!= null)
iis.close();
}
}
/**
* inputfilepathの接尾辞名を取得します。たとえば、 "e:/test.pptx" with suffix name: "pptx" <br>
*
* @param inputfilepath
* @戻る
*/
public string getPostfix(String inputFilepath){
inputfilepath.substring(inputfilepath.lastindexof( "。") + 1)を返します。
}
public int getheight(){
戻り高さ;
}
public void setheight(int height){
this.height = height;
}
public string getsrcpath(){
srcpathを返します。
}
public void setsrcpath(string srcpath){
this.srcpath = srcpath;
}
public string getsubpath(){
サブパスを返します。
}
public void setsubpath(string subpath){
this.subpath = subpath;
}
public int getWidth(){
戻り幅;
}
public void setWidth(int width){
this.width = width;
}
public int getX(){
xを返します。
}
public void setx(int x){
this.x = x;
}
public int gety(){
yを返します。
}
public void sety(int y){
this.y = y;
}
}