图片改变像素,宽高,Base64编码处理

2024-04-30 19:38

本文主要是介绍图片改变像素,宽高,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编码处理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/949744

相关文章

Python利用PIL进行图片压缩

《Python利用PIL进行图片压缩》有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所以本文为大家介绍了Python中图片压缩的方法,需要的可以参考下... 有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所有可以对文件中的图

Java文件与Base64之间的转化方式

《Java文件与Base64之间的转化方式》这篇文章介绍了如何使用Java将文件(如图片、视频)转换为Base64编码,以及如何将Base64编码转换回文件,通过提供具体的工具类实现,作者希望帮助读者... 目录Java文件与Base64之间的转化1、文件转Base64工具类2、Base64转文件工具类3、

java获取图片的大小、宽度、高度方式

《java获取图片的大小、宽度、高度方式》文章介绍了如何将File对象转换为MultipartFile对象的过程,并分享了个人经验,希望能为读者提供参考... 目China编程录Java获取图片的大小、宽度、高度File对象(该对象里面是图片)MultipartFile对象(该对象里面是图片)总结java获取图片

使用C++将处理后的信号保存为PNG和TIFF格式

《使用C++将处理后的信号保存为PNG和TIFF格式》在信号处理领域,我们常常需要将处理结果以图像的形式保存下来,方便后续分析和展示,C++提供了多种库来处理图像数据,本文将介绍如何使用stb_ima... 目录1. PNG格式保存使用stb_imagephp_write库1.1 安装和包含库1.2 代码解

Java实战之自助进行多张图片合成拼接

《Java实战之自助进行多张图片合成拼接》在当今数字化时代,图像处理技术在各个领域都发挥着至关重要的作用,本文为大家详细介绍了如何使用Java实现多张图片合成拼接,需要的可以了解下... 目录前言一、图片合成需求描述二、图片合成设计与实现1、编程语言2、基础数据准备3、图片合成流程4、图片合成实现三、总结前

Python利用自带模块实现屏幕像素高效操作

《Python利用自带模块实现屏幕像素高效操作》这篇文章主要为大家详细介绍了Python如何利用自带模块实现屏幕像素高效操作,文中的示例代码讲解详,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、获取屏幕放缩比例2、获取屏幕指定坐标处像素颜色3、一个简单的使用案例4、总结1、获取屏幕放缩比例from

使用Python实现图片和base64转换工具

《使用Python实现图片和base64转换工具》这篇文章主要为大家详细介绍了如何使用Python中的base64模块编写一个工具,可以实现图片和Base64编码之间的转换,感兴趣的小伙伴可以了解下... 简介使用python的base64模块来实现图片和Base64编码之间的转换。可以将图片转换为Bas

C#使用DeepSeek API实现自然语言处理,文本分类和情感分析

《C#使用DeepSeekAPI实现自然语言处理,文本分类和情感分析》在C#中使用DeepSeekAPI可以实现多种功能,例如自然语言处理、文本分类、情感分析等,本文主要为大家介绍了具体实现步骤,... 目录准备工作文本生成文本分类问答系统代码生成翻译功能文本摘要文本校对图像描述生成总结在C#中使用Deep

css实现图片旋转功能

《css实现图片旋转功能》:本文主要介绍了四种CSS变换效果:图片旋转90度、水平翻转、垂直翻转,并附带了相应的代码示例,详细内容请阅读本文,希望能对你有所帮助... 一 css实现图片旋转90度.icon{ -moz-transform:rotate(-90deg); -webkit-transfo

Spring Boot 整合 ShedLock 处理定时任务重复执行的问题小结

《SpringBoot整合ShedLock处理定时任务重复执行的问题小结》ShedLock是解决分布式系统中定时任务重复执行问题的Java库,通过在数据库中加锁,确保只有一个节点在指定时间执行... 目录前言什么是 ShedLock?ShedLock 的工作原理:定时任务重复执行China编程的问题使用 Shed