package com.aast.utils;
import static org.bytedeco.javacpp.opencv_core.flip;
import static org.bytedeco.javacpp.opencv_imgcodecs.IMREAD_COLOR;
import static org.bytedeco.javacpp.opencv_imgcodecs.imread;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.bytedeco.javacpp.opencv_core.Mat;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter.ToMat;
/**功能说明:JavaCV1.1 mat读取/显示工具类
*
*/
public class JavaCVUtil {
/**获取本地图片路径
* @return
*/
public static String openImage(){
String imgPath=null;
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter =
new FileNameExtensionFilter("图片文件", "jpg", "gif","bmp","jpeg","png");
chooser.setFileFilter(filter); // 文件过滤器可以不要
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
imgPath=chooser.getSelectedFile().getPath();
}
return imgPath;
}
/**图像翻转
* @param imgPath 图片路径
* @return
*/
public static Mat flipCode(Mat image){
Mat result = new Mat();
int flipCode=1;
//flipCode ,>0 水平翻转 ,=0 垂直翻转 , <0 同时翻转
flip(image, result, flipCode);
//显示处理过的图像
return result;
}
/**
* 功能说明:显示图像
* @param mat
*
*/
public static CanvasFrame imShow(Mat mat,CanvasFrame canvas) {
ToMat converter = new OpenCVFrameConverter.ToMat();
canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas.showImage(converter.convert(mat));
return canvas;
}
/**
*
* 功能说明:读取图像
* @param filePath
* 文件路径,可以包含中文
* @return Mat
*
*/
public static Mat imRead(String filePath){
Mat mat = null;
File file=new File(filePath);
try {
//使用java2D读取图像
BufferedImage image = ImageIO.read(file);
/**
* 转为mat
* 1、由Java2D的image转为javacv自定义的Frame
* 2、由Frame转为Mat
*/
Java2DFrameConverter java2dFrameConverter = new Java2DFrameConverter();
Frame frame = java2dFrameConverter.convert(image);
ToMat converter = new OpenCVFrameConverter.ToMat();
mat = converter.convert(frame);
} catch (Exception e) {
System.out.println("读取图像出现异常:filePath="+filePath);
e.printStackTrace();
}
return mat;
}
/**
*
* 功能说明:保存mat到指定路径
* @param mat
* 要保存的Mat
* @param filePath
* 保存路径
*
*/
public static boolean imWrite(Mat mat,String filePath){
try {
/**
* 将mat转为java的BufferedImage
*/
ToMat convert= new ToMat();
Frame frame= convert.convert(mat);
Java2DFrameConverter java2dFrameConverter = new Java2DFrameConverter();
BufferedImage bufferedImage= java2dFrameConverter.convert(frame);
ImageIO.write(bufferedImage, "PNG", new File(filePath));
return true;
} catch (Exception e) {
System.out.println("保存文件出现异常:"+filePath);
e.printStackTrace();
}
return false;
}
}
本体图片加载到内存,以及调整图片大小
package com.aast.utils;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.ImageProducer;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class ImageUtils {
/**缩放图片
* @param imageIcon
* @param width
* @param height
* @return
*/
public static ImageIcon setImgSize(ImageIcon imageIcon,int width,int height){
Image image = imageIcon.getImage(); // 但这个图片太大不适合做Icon
// 为把它缩小点,先要取出这个Icon的image ,然后缩放到合适的大小
Image smallImage = image.getScaledInstance(width,height,Image.SCALE_FAST);
// 再由修改后的Image来生成合适的Icon
ImageIcon Icon = new ImageIcon(smallImage);
return Icon;
}
/**加载本地图片
* @param imgstr
* @return
*/
public static ImageIcon useBMP(String imgstr){
ImageIcon ic=null;
try {
BufferedImage bi= ImageIO.read(new File(imgstr));
ImageProducer producer=bi.getSource();
Toolkit toolkit=Toolkit.getDefaultToolkit();
Image image=toolkit.createImage(producer);
ic=new ImageIcon(image);
} catch (IOException e) {
e.printStackTrace();
}
return ic;
}
/**加载本地图片
* @param imgPath
* @return
*/
public static byte[] readImageInfo(String imgPath){
File bmpfile=new File(imgPath);
if(!bmpfile.exists()){
return null;
}
BufferedImage img=null;
ByteArrayOutputStream bf=null;
try {
bf=new ByteArrayOutputStream();
img=ImageIO.read(bmpfile);
ImageIO.write(img,"bmp",bf);
} catch (IOException e1) {
e1.printStackTrace();
}
return bf.toByteArray();
}
/**加载本地图片
* @param path
* @return
*/
public static byte[] getPhoto(String path){
File bmpfile=new File(path);
if(!bmpfile.exists()){
return null;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream((int)bmpfile.length());
BufferedInputStream in = null;
try{
in = new BufferedInputStream(new FileInputStream(bmpfile));
int buf_size = 1024;
byte[] buffer = new byte[buf_size];
int len = 0;
while((len = in.read(buffer,0,buf_size))!=-1){
bos.write(buffer,0,len);
}
}catch (IOException e) {
e.printStackTrace();
}finally{
try{
in.close();
bos.close();
}catch (IOException e) {
e.printStackTrace();
}
}
return bos.toByteArray();
}
/**byte[]图片转Image
* @param buf
* @return
*/
public static Image byteToImage(byte[] buf){
BufferedImage img=null;
try {
img= ImageIO.read(new ByteArrayInputStream(buf));
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
/**获取当前jar包路径
* @return
*/
public static String getProjectPath() {
java.net.URL url = ImageUtils.class.getProtectionDomain().getCodeSource()
.getLocation();
String filePath = null;
try {
filePath = java.net.URLDecoder.decode(url.getPath(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
if (filePath.endsWith(".jar"))
filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1);
java.io.File file = new java.io.File(filePath);
filePath = file.getAbsolutePath();
return filePath;
}
}