1,下载网络图片
package org.jeecg.modules.ppl.utils;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
/**
* 下载网络图片
*/
public class PngUtil{
/**
* 通过链接url下载图片
* inImageUrl 网络图片url
* putImagePath 保存的本地路径
*/
public static String downloadPic(String inImageUrl,String putImagePath) {
URL url = null;
int imageNumber = 0;
try {
url = new URL(inImageUrl);
DataInputStream dataInputStream = new DataInputStream(url.openStream());
putImagePath = "E:/upload/media/lanzhou2.png";
FileOutputStream fileOutputStream = new FileOutputStream(new File(putImagePath));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
byte[] context = output.toByteArray();
fileOutputStream.write(output.toByteArray());
dataInputStream.close();
fileOutputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
putImagePath = "错误:" + e.getMessage();
} catch (IOException e) {
e.printStackTrace();
putImagePath = "错误:" + e.getMessage();
}
return putImagePath;
}
}
2,图片转换成透明底图片(推荐)
package org.jeecg.modules.ppl.utils;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.FileImageOutputStream;
import javax.swing.ImageIcon;
/**
* 生成透明图片
*/
public class PngUtil{
/**
* 通过链接url下载图片
* inImageUrl 网络图片url
* putImagePath 保存的本地路径
*/
public static String downloadPic(String inImageUrl,String putImagePath) {
URL url = null;
int imageNumber = 0;
try {
url = new URL(inImageUrl);
DataInputStream dataInputStream = new DataInputStream(url.openStream());
putImagePath = "E:/upload/media/lanzhou2.png";
FileOutputStream fileOutputStream = new FileOutputStream(new File(putImagePath));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
byte[] context = output.toByteArray();
fileOutputStream.write(output.toByteArray());
dataInputStream.close();
fileOutputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
putImagePath = "错误:" + e.getMessage();
} catch (IOException e) {
e.printStackTrace();
putImagePath = "错误:" + e.getMessage();
}
return putImagePath;
}
/**
* @param imgSrc 原图片地址
* @param imgTarget 新图片地址
* @return 转换结果ture or false
*/
public static boolean transferAlpha2File(String imgSrc, String imgTarget) {
File file = new File(imgSrc);
InputStream is = null;
boolean result = false;
try {
is = new FileInputStream(file);
// 如果是MultipartFile类型,那么自身也有转换成流的方法:is = file.getInputStream();
BufferedImage bi = ImageIO.read(is);
Image image = (Image) bi;
ImageIcon imageIcon = new ImageIcon(image);
BufferedImage bufferedImage = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(),BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());
int alpha = 0;
for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage.getHeight(); j1++) {
for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage.getWidth(); j2++) {
int rgb = bufferedImage.getRGB(j2, j1);
int R = (rgb & 0xff0000) >> 16;
int G = (rgb & 0xff00) >> 8;
int B = (rgb & 0xff);
if (((255 - R) < 30) && ((255 - G) < 30) && ((255 - B) < 30)) {
rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff);
}
bufferedImage.setRGB(j2, j1, rgb);
}
}
g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());
result = ImageIO.write(bufferedImage, "png", new File(imgTarget));// 直接输出文件
// result = ImageIO.write(bufferedImage, "jpg", new File(imgTarget));// 直接输出文件
} catch (Exception e) {
e.printStackTrace();
result = false;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
return result;
}
/**
* 透明图片的byte数组
* @param imgSrc
* @return
*/
public static byte[] transferAlpha2Byte(String imgSrc) {
ByteArrayOutputStream byteArrayOutputStream = null;
File file = new File(imgSrc);
InputStream is = null;
byte[] result = null;
try {
is = new FileInputStream(file);
// 如果是MultipartFile类型,那么自身也有转换成流的方法:is = file.getInputStream();
BufferedImage bi = ImageIO.read(is);
Image image = (Image) bi;
ImageIcon imageIcon = new ImageIcon(image);
BufferedImage bufferedImage = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(),BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());
int alpha = 0;
for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage.getHeight(); j1++) {
for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage.getWidth(); j2++) {
int rgb = bufferedImage.getRGB(j2, j1);
int R = (rgb & 0xff0000) >> 16;
int G = (rgb & 0xff00) >> 8;
int B = (rgb & 0xff);
if (((255 - R) < 30) && ((255 - G) < 30) && ((255 - B) < 30)) {
rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff);
}
bufferedImage.setRGB(j2, j1, rgb);
}
}
g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());
byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", byteArrayOutputStream);// 转换成byte数组
result = byteArrayOutputStream.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
if (byteArrayOutputStream != null) {
try {
byteArrayOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
return result;
}
/**
* byte数组到图片
*/
public static void byteToImage(byte[] data,String path){
if (data.length < 3 || path.equals("")) return;
try {
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
imageOutput.write(data, 0, data.length);
imageOutput.close();
} catch(Exception ex) {
System.out.println("Exception: " + ex.getMessage());
ex.printStackTrace();
}
}
/**
* 图片转化为byte数组
* @param path
* @return
*/
public byte[] imageToByte(String path){
byte[] data = null;
FileImageInputStream input = null;
try {
input = new FileImageInputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numBytesRead = 0;
while ((numBytesRead = input.read(buf)) != -1) {
output.write(buf, 0, numBytesRead);
}
data = output.toByteArray();
output.close();
input.close();
} catch (FileNotFoundException ex1) {
ex1.printStackTrace();
} catch (IOException ex1) {
ex1.printStackTrace();
}
return data;
}
/**
* 图片转base64编码
* 拼接 "data:image/png;base64,"
*/
public static String imageToBase64(String path){
byte[] bytes = imageToByte(path);
String base64String = Base64.encodeBase64String(bytes);
return base64String;
}
}
3,图片转换成透明底图片(不推荐,图片会模糊)
package org.jeecg.modules.ppl.utils;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
/**
* 生成透明图片
*/
public class PngUtil{
//色差范围0~255
public static int color_range = 20;
public static void opacityPng(String imgPath) throws IOException {
BufferedImage image = ImageIO.read(new File(imgPath));
// 高度和宽度
int height = image.getHeight();
int width = image.getWidth();
// 生产背景透明和内容透明的图片
ImageIcon imageIcon = new ImageIcon(image);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics(); // 获取画笔
g2D.drawImage(imageIcon.getImage(), 0, 0, null); // 绘制Image的图片,使用了imageIcon.getImage(),目的就是得到image,直接使用image就可以的
int alpha = 0; // 图片透明度
// 外层遍历是Y轴的像素
for (int y = bufferedImage.getMinY(); y < bufferedImage.getHeight(); y++) {
// 内层遍历是X轴的像素
for (int x = bufferedImage.getMinX(); x < bufferedImage.getWidth(); x++) {
int rgb = bufferedImage.getRGB(x, y);
// 对当前颜色判断是否在指定区间内
if (colorInRange(rgb)){
alpha = 0;
}else{
// 设置为不透明
alpha = 255;
}
// #AARRGGBB 最前两位为透明度
rgb = (alpha << 24) | (rgb & 0x00ffffff);
bufferedImage.setRGB(x, y, rgb);
}
}
// 绘制设置了RGB的新图片,这一步感觉不用也可以只是透明地方的深浅有变化而已,就像蒙了两层的感觉
g2D.drawImage(bufferedImage, 0, 0, null);
// 生成图片为PNG
ImageIO.write(bufferedImage, "png", new File("E:/upload/media/lanzhou1.png"));
}
// 判断是背景还是内容
public static boolean colorInRange(int color) {
int red = (color & 0xff0000) >> 16;// 获取color(RGB)中R位
int green = (color & 0x00ff00) >> 8;// 获取color(RGB)中G位
int blue = (color & 0x0000ff); // 获取color(RGB)中B位
// 通过RGB三分量来判断当前颜色是否在指定的颜色区间内
if (red >= color_range && green >= color_range && blue >= color_range){
return true;
};
return false;
}
/**
* 通过链接url下载图片
* inImageUrl 网络图片url
* putImagePath 保存的本地路径
*/
public static String downloadPic(String inImageUrl,String putImagePath) {
URL url = null;
int imageNumber = 0;
try {
url = new URL(inImageUrl);
DataInputStream dataInputStream = new DataInputStream(url.openStream());
putImagePath = "E:/upload/media/lanzhou2.png";
FileOutputStream fileOutputStream = new FileOutputStream(new File(putImagePath));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
byte[] context = output.toByteArray();
fileOutputStream.write(output.toByteArray());
dataInputStream.close();
fileOutputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
putImagePath = "错误:" + e.getMessage();
} catch (IOException e) {
e.printStackTrace();
putImagePath = "错误:" + e.getMessage();
}
return putImagePath;
}
}