package com.adam.dev.pic.easyImage;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class OperateImage{
public OperateImage() {
super();
}
public void cropImage(String srcPath,String toPath,
int x,int y,int width,int height,
String readImageFormat,String writeImageFormat) throws IOException{
FileInputStream fis = null ;
ImageInputStream iis =null ;
try{
fis = new FileInputStream(srcPath);
Iterator it = ImageIO.getImageReadersByFormatName(readImageFormat);
ImageReader reader = (ImageReader) it.next();
iis = ImageIO.createImageInputStream(fis);
reader.setInput(iis,true) ;
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(x, y, width, height);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0,param);
ImageIO.write(bi, writeImageFormat, new File(toPath));
}finally{
if(fis!=null)
fis.close();
if(iis!=null)
iis.close();
}
}
public void reduceImageByRatio(String srcImagePath,String toImagePath,int widthRatio,int heightRatio) throws IOException{
FileOutputStream out = null;
try{
File file = new File(srcImagePath);
BufferedImage src = javax.imageio.ImageIO.read(file);
int width = src.getWidth();
int height = src.getHeight();
BufferedImage tag = new BufferedImage(width / widthRatio, height / heightRatio, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, width / widthRatio, height / heightRatio, null);
out = new FileOutputStream(toImagePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
}catch(Exception e){
e.printStackTrace();
}finally{
if(out != null){
out.close();
}
}
}
public void reduceImageEqualProportion(String srcImagePath,String toImagePath,int ratio) throws IOException{
FileOutputStream out = null;
try{
File file = new File(srcImagePath);
BufferedImage src = javax.imageio.ImageIO.read(file);
int width = src.getWidth();
int height = src.getHeight();
BufferedImage tag = new BufferedImage(width / ratio, height / ratio, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, width / ratio, height / ratio, null);
out = new FileOutputStream(toImagePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
}catch(Exception e){
e.printStackTrace();
}finally{
if(out != null){
out.close();
}
}
}
public void enlargementImageByRatio(String srcImagePath,String toImagePath,int widthRatio,int heightRatio) throws IOException{
FileOutputStream out = null;
try{
File file = new File(srcImagePath);
BufferedImage src = javax.imageio.ImageIO.read(file);
int width = src.getWidth();
int height = src.getHeight();
BufferedImage tag = new BufferedImage(width * widthRatio, height * heightRatio, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, width * widthRatio, height * heightRatio, null);
out = new FileOutputStream(toImagePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
}catch(Exception e){
e.printStackTrace();
}finally{
if(out != null){
out.close();
}
}
}
public void enlargementImageEqualProportion(String srcImagePath,String toImagePath,int ratio) throws IOException{
FileOutputStream out = null;
try{
File file = new File(srcImagePath);
BufferedImage src = javax.imageio.ImageIO.read(file);
int width = src.getWidth();
int height = src.getHeight();
BufferedImage tag = new BufferedImage(width * ratio, height * ratio, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, width * ratio, height * ratio, null);
out = new FileOutputStream(toImagePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
}catch(Exception e){
e.printStackTrace();
}finally{
if(out != null){
out.close();
}
}
}
public void resizeImage(String srcImagePath,String toImagePath,int width,int height) throws IOException{
FileOutputStream out = null;
try{
File file = new File(srcImagePath);
BufferedImage src = javax.imageio.ImageIO.read(file);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, width, height, null);
out = new FileOutputStream(toImagePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
}catch(Exception e){
e.printStackTrace();
}finally{
if(out != null){
out.close();
}
}
}
public void joinImagesHorizontal(String firstSrcImagePath, String secondSrcImagePath,String imageFormat, String toPath){
try {
File fileOne = new File(firstSrcImagePath);
BufferedImage imageOne = ImageIO.read(fileOne);
int width = imageOne.getWidth();
int height = imageOne.getHeight();
int[] imageArrayOne = new int[width*height];
imageArrayOne = imageOne.getRGB(0,0,width,height,imageArrayOne,0,width);
File fileTwo = new File(secondSrcImagePath);
BufferedImage imageTwo = ImageIO.read(fileTwo);
int width2 = imageTwo.getWidth();
int height2 = imageTwo.getHeight();
int[] ImageArrayTwo = new int[width2*height2];
ImageArrayTwo = imageTwo.getRGB(0,0,width,height,ImageArrayTwo,0,width);
BufferedImage imageNew = new BufferedImage(width*2,height,BufferedImage.TYPE_INT_RGB);
imageNew.setRGB(0,0,width,height,imageArrayOne,0,width);
imageNew.setRGB(width,0,width,height,ImageArrayTwo,0,width);
File outFile = new File(toPath);
ImageIO.write(imageNew, imageFormat, outFile);
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean joinImageListHorizontal(String[] pics, String type, String dst_pic) {
try {
int len = pics.length;
if (len < 1) {
System.out.println("pics len < 1");
return false;
}
File[] src = new File[len];
BufferedImage[] images = new BufferedImage[len];
int[][] imageArrays = new int[len][];
for (int i = 0; i < len; i++) {
src[i] = new File(pics[i]);
images[i] = ImageIO.read(src[i]);
int width = images[i].getWidth();
int height = images[i].getHeight();
imageArrays[i] = new int[width * height];
imageArrays[i] = images[i].getRGB(0, 0, width, height, imageArrays[i], 0, width);
}
int dst_width = 0;
int dst_height = images[0].getHeight();
for (int i = 0; i < images.length; i++) {
dst_height = dst_height > images[i].getHeight() ? dst_height : images[i].getHeight();
dst_width += images[i].getWidth();
}
if (dst_height < 1) {
System.out.println("dst_height < 1");
return false;
}
BufferedImage ImageNew = new BufferedImage(dst_width, dst_height, BufferedImage.TYPE_INT_RGB);
int width_i = 0;
for (int i = 0; i < images.length; i++) {
ImageNew.setRGB(width_i, 0, images[i].getWidth(), dst_height, imageArrays[i], 0, images[i].getWidth());
width_i += images[i].getWidth();
}
File outFile = new File(dst_pic);
ImageIO.write(ImageNew, type, outFile);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public void joinImagesVertical(String firstSrcImagePath, String secondSrcImagePath,String imageFormat, String toPath){
try {
File fileOne = new File(firstSrcImagePath);
BufferedImage imageOne = ImageIO.read(fileOne);
int width = imageOne.getWidth();
int height = imageOne.getHeight();
int[] imageArrayOne = new int[width*height];
imageArrayOne = imageOne.getRGB(0,0,width,height,imageArrayOne,0,width);
File fileTwo = new File(secondSrcImagePath);
BufferedImage imageTwo = ImageIO.read(fileTwo);
int width2 = imageTwo.getWidth();
int height2 = imageTwo.getHeight();
int[] ImageArrayTwo = new int[width2*height2];
ImageArrayTwo = imageTwo.getRGB(0,0,width,height,ImageArrayTwo,0,width);
BufferedImage imageNew = new BufferedImage(width,height*2,BufferedImage.TYPE_INT_RGB);
imageNew.setRGB(0,0,width,height,imageArrayOne,0,width);
imageNew.setRGB(0,height,width,height,ImageArrayTwo,0,width);
File outFile = new File(toPath);
ImageIO.write(imageNew, imageFormat, outFile);
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean joinImageListVertical(String[] pics, String type, String dst_pic) {
try {
int len = pics.length;
if (len < 1) {
System.out.println("pics len < 1");
return false;
}
File[] src = new File[len];
BufferedImage[] images = new BufferedImage[len];
int[][] imageArrays = new int[len][];
for (int i = 0; i < len; i++) {
src[i] = new File(pics[i]);
images[i] = ImageIO.read(src[i]);
int width = images[i].getWidth();
int height = images[i].getHeight();
imageArrays[i] = new int[width * height];
imageArrays[i] = images[i].getRGB(0, 0, width, height, imageArrays[i], 0, width);
}
int dst_height = 0;
int dst_width = images[0].getWidth();
for (int i = 0; i < images.length; i++) {
dst_width = dst_width > images[i].getWidth() ? dst_width : images[i].getWidth();
dst_height += images[i].getHeight();
}
if (dst_height < 1) {
System.out.println("dst_height < 1");
return false;
}
BufferedImage ImageNew = new BufferedImage(dst_width, dst_height, BufferedImage.TYPE_INT_RGB);
int height_i = 0;
for (int i = 0; i < images.length; i++) {
ImageNew.setRGB(0, height_i, dst_width, images[i].getHeight(), imageArrays[i], 0, dst_width);
height_i += images[i].getHeight();
}
File outFile = new File(dst_pic);
ImageIO.write(ImageNew, type, outFile);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public void mergeBothImage(String negativeImagePath,String additionImagePath,int x,int y,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,x,y,null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
public void mergeImageList(String negativeImagePath,List additionImageList,String imageFormat, String toPath) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
BufferedImage image=ImageIO.read(is);
Graphics2D g = image.createGraphics();;
BufferedImage image2 = null;
if(additionImageList != null){
for(int i=0;i<additionImageList.size();i++){
String[] additionImageInfo = (String[]) additionImageList.get(i);
int x = Integer.parseInt(additionImageInfo[0]);
int y = Integer.parseInt(additionImageInfo[1]);
String additionImagePath = additionImageInfo[2];
is2 = new FileInputStream(additionImagePath);
image2 = ImageIO.read(is2);
g.drawImage(image2,x,y,null);
}
}
os = new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, os);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
public void mergeBothImageTopleftcorner(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,0,0,null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
public void mergeBothImageToprightcorner(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,image.getWidth()-image2.getWidth(),0,null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
public void mergeBothImageLeftbottom(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,0,image.getHeight()-image2.getHeight(),null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
public void mergeBothImageRightbottom(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,image.getWidth()-image2.getWidth(),image.getHeight()-image2.getHeight(),null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
public void mergeBothImageCenter(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,image.getWidth()/2-image2.getWidth()/2,image.getHeight()/2-image2.getHeight()/2,null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
public void mergeBothImageTopcenter(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,image.getWidth()/2-image2.getWidth()/2,0,null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
public void mergeBothImageBottomcenter(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,image.getWidth()/2-image2.getWidth()/2,image.getHeight()-image2.getHeight(),null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
public void mergeBothImageLeftcenter(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,0,image.getHeight()/2-image2.getHeight()/2,null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
public void mergeBothImageRightcenter(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,image.getWidth()-image2.getWidth(),image.getHeight()/2-image2.getHeight()/2,null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
public void grayImage(String srcImage,String toPath,String imageFormat){
try{
BufferedImage src = ImageIO.read(new File(srcImage));
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(cs, null);
src = op.filter(src, null);
ImageIO.write(src, imageFormat, new File(toPath));
}catch(Exception e){
e.printStackTrace();
}
}
public void alphaWords2Image(String srcImagePath,float alpha,
String font,int fontStyle,int fontSize,Color color,
String inputWords,int x,int y,String imageFormat,String toPath) throws IOException{
FileOutputStream fos=null;
try {
BufferedImage image = ImageIO.read(new File(srcImagePath));
Graphics2D g2d=image.createGraphics();
g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null, null);
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
g2d.setComposite(ac);
g2d.setFont(new Font(font, fontStyle, fontSize));
g2d.setColor(color);
g2d.drawString(inputWords, x, y);
g2d.dispose();
fos=new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, fos);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fos!=null){
fos.close();
}
}
}
public void alphaImage2Image(String srcImagePath,String appendImagePath,
float alpha,int x,int y,int width,int height,
String imageFormat,String toPath) throws IOException{
FileOutputStream fos = null;
try {
BufferedImage image = ImageIO.read(new File(srcImagePath));
Graphics2D g2d=image.createGraphics();
g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null, null);
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
g2d.setComposite(ac);
BufferedImage appendImage = ImageIO.read(new File(appendImagePath));
g2d.drawImage(appendImage, x, y, width, height, null, null);
g2d.dispose();
fos=new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, fos);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fos!=null){
fos.close();
}
}
}
public void drawPoint(String srcImagePath,int x,int y,int width,int height,Color ovalColor,String imageFormat,String toPath) throws IOException{
FileOutputStream fos = null;
try {
BufferedImage image = ImageIO.read(new File(srcImagePath));
Graphics2D g2d = image.createGraphics();
g2d.setColor(ovalColor);
g2d.fillOval(x, y, width, height);
fos = new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, fos);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fos!=null){
fos.close();
}
}
}
public void drawPoints(String srcImagePath,List pointList,int width,int height,Color ovalColor,String imageFormat,String toPath) throws IOException{
FileOutputStream fos = null;
try {
BufferedImage image = ImageIO.read(new File(srcImagePath));
Graphics2D g2d = image.createGraphics();
g2d.setColor(ovalColor);
if(pointList != null){
for(int i=0;i<pointList.size();i++){
Point point = (Point)pointList.get(i);
int x = (int) point.getX();
int y = (int) point.getY();
g2d.fillOval(x, y, width, height);
}
}
fos = new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, fos);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fos!=null){
fos.close();
}
}
}
public void drawLine(String srcImagePath,int x1,int y1,int x2,int y2, Color lineColor,String toPath,String imageFormat) throws IOException{
FileOutputStream fos = null;
try {
BufferedImage image = ImageIO.read(new File(srcImagePath));
Graphics2D g2d = image.createGraphics();
g2d.setColor(lineColor);
g2d.drawLine( x1, y1, x2, y2);
fos = new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, fos);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fos!=null){
fos.close();
}
}
}
public void drawPolyline(String srcImagePath,int[] xPoints, int[] yPoints, int nPoints,Color lineColor,String toPath,String imageFormat) throws IOException{
FileOutputStream fos = null;
try {
BufferedImage image = ImageIO.read(new File(srcImagePath));
Graphics2D g2d = image.createGraphics();
g2d.setColor(lineColor);
g2d.drawPolyline(xPoints, yPoints, nPoints);
fos = new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, fos);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fos!=null){
fos.close();
}
}
}
public void drawPolylineShowPoints(String srcImagePath,int[] xPoints, int[] yPoints, int nPoints,Color lineColor,int width,int height,Color ovalColor,String toPath,String imageFormat) throws IOException{
FileOutputStream fos = null;
try {
BufferedImage image = ImageIO.read(new File(srcImagePath));
Graphics2D g2d = image.createGraphics();
g2d.setColor(lineColor);
g2d.drawPolyline(xPoints, yPoints, nPoints);
g2d.setColor(ovalColor);
if(xPoints != null){
for(int i=0;i<xPoints.length;i++){
int x = xPoints[i];
int y = yPoints[i];
g2d.fillOval(x, y, width, height);
}
}
fos = new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, fos);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fos!=null){
fos.close();
}
}
}
public void drawPolygon(String srcImagePath,int[] xPoints,int[] yPoints,int nPoints,Color polygonColor,String imageFormat,String toPath) throws IOException {
FileOutputStream fos = null;
try {
BufferedImage image = ImageIO.read(new File(srcImagePath));
Graphics2D g2d = image.createGraphics();
g2d.setColor(polygonColor);
g2d.drawPolygon(xPoints, yPoints, nPoints);
fos = new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, fos);
g2d.dispose();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fos!=null){
fos.close();
}
}
}
public void drawAndAlphaPolygon(String srcImagePath,int[] xPoints,int[] yPoints,int nPoints,Color polygonColor,float alpha,String imageFormat,String toPath) throws IOException{
FileOutputStream fos = null;
try {
BufferedImage image = ImageIO.read(new File(srcImagePath));
Graphics2D g2d = image.createGraphics();
g2d.setColor(polygonColor);
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
g2d.setComposite(ac);
g2d.fillPolygon(xPoints, yPoints, nPoints);
fos = new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, fos);
g2d.dispose();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fos!=null){
fos.close();
}
}
}
public static void main(String[] args)throws Exception{
OperateImage imageObj = new OperateImage();
;
String str = "北京\n上海\n广州\n深圳";
System.out.println(str);
String path = "c:/relevantdata.txt";
FileOutputStream fops = new FileOutputStream(path);
fops.write(str.getBytes());
BufferedReader inputStream = new BufferedReader(new FileReader(new File(path)));
String mrMsg = "";
while((mrMsg = inputStream.readLine())!=null){
System.out.println(mrMsg);
}
}
}