本文主要是介绍图片改变像素,宽高,Base64编码处理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.改变图片像素
private void setAlpha(String os) {
/**
* 增加测试项
* 读取图片,绘制成半透明,修改像素
*/
try {
ImageIcon imageIcon = new ImageIcon(os);
BufferedImage bufferedImage = new BufferedImage(imageIcon.getIconWidth(),imageIcon.getIconHeight()
, BufferedImage.TYPE_USHORT_565_RGB);
Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
g2D.drawImage(imageIcon.getImage(), 0, 0,
imageIcon.getImageObserver());
//循环每一个像素点,改变像素点的Alpha值
int alpha = 100;
System.out.println(System.currentTimeMillis());
for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage.getHeight(); j1++) {
for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage.getWidth(); j2++) {
int pixel = bufferedImage.getRGB(j2, j1);
int[] rgb = new int[3];
rgb[0] = (pixel & 0xff0000) >> 16;
rgb[1] = (pixel & 0xff00) >> 8;
rgb[2] = (pixel & 0xff);
pixel = ( (alpha + 1) << 24) | (pixel & 0x00ffffff);
bufferedImage.setRGB(j2, j1, pixel);
}
}
System.out.println(System.currentTimeMillis());
g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());
//生成图片为PNG
ImageIO.write(bufferedImage, "jpg", new File("C:\\Desktop\\1.jpg"));
}
catch (Exception e) {
e.printStackTrace();
}
}
2.改变图片宽高
/**
* 按指定高度 等比例缩放图片
*
* @param imageFile
* @param newPath
* @param newWidth 新图的宽度
* @throws IOException
*/
public static void zoomImageScale(File imageFile, String newPath, int newWidth) throws IOException {
System.out.println("------------------------------------------------------------------");
if(!imageFile.canRead())
return;
BufferedImage bufferedImage = ImageIO.read(imageFile);
if (null == bufferedImage)
return;
int originalWidth = bufferedImage.getWidth();
int originalHeight = bufferedImage.getHeight();
double scale = (double)originalWidth / (double)newWidth; // 缩放的比例
int newHeight = (int)(originalHeight / scale);
zoomImageUtils(imageFile, newPath, bufferedImage, newWidth, newHeight);
}
private static void zoomImageUtils(File imageFile, String newPath, BufferedImage bufferedImage, int width, int height)
throws IOException{
String suffix = StringUtils.substringAfterLast(imageFile.getName(), ".");
// 处理 png 背景变黑的问题
if(suffix != null && (suffix.trim().toLowerCase().endsWith("png") || suffix.trim().toLowerCase().endsWith("gif"))){
BufferedImage to= new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = to.createGraphics();
to = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
g2d.dispose();
g2d = to.createGraphics();
System.out.println(width+"---"+height+"------------------------------------------------------------------"+Image.SCALE_AREA_AVERAGING);
Image from = bufferedImage.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
g2d.drawImage(from, 0, 0, null);
g2d.dispose();
ImageIO.write(to, suffix, new File(newPath));
}else{
System.out.println("------------------------------------------------------------------");
// 高质量压缩,其实对清晰度而言没有太多的帮助
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(bufferedImage, 0, 0, width, height, null);
FileOutputStream out = new FileOutputStream(newPath); // 将图片写入 newPath
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
jep.setQuality(1f, true); //压缩质量, 1 是最高值
encoder.encode(tag, jep);
out.close();
BufferedImage newImage = new BufferedImage(width, height, bufferedImage.getType());
Graphics g = newImage.getGraphics();
g.drawImage(bufferedImage, 0, 0, width, height, null);
g.dispose();
ImageIO.write(newImage, suffix, new File(newPath));
}
}
3.将图片文件转化为字节数组字符串,并对其进行Base64编码处理
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.*;
/**
* @author hhr
* @create 2017-09-08
**/
public class Base64Test {
public static void main(String[] args) {
String strImg = GetImageStr();
System.out.println(strImg);
GenerateImage(strImg);
}
//图片转化成base64字符串
public static String GetImageStr() {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
String imgFile = "C:\\Users\\Administrator\\Desktop\\1.png";//待处理的图片
InputStream in = null;
byte[] data = null;
//读取图片字节数组
try {
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
//对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);//返回Base64编码过的字节数组字符串
}
//base64字符串转化成图片
public static boolean GenerateImage(String imgStr) { //对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) //图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
try {
//Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {//调整异常数据
b[i] += 256;
}
}
//生成jpeg图片
String imgFilePath = "C://222.jpg";//新生成的图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {return false;}
这篇关于图片改变像素,宽高,Base64编码处理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!