图片改变像素,宽高,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+FFmpeg实现视频自动化处理的完整指南

《Python+FFmpeg实现视频自动化处理的完整指南》本文总结了一套在Python中使用subprocess.run调用FFmpeg进行视频自动化处理的解决方案,涵盖了跨平台硬件加速、中间素材处理... 目录一、 跨平台硬件加速:统一接口设计1. 核心映射逻辑2. python 实现代码二、 中间素材处

Go异常处理、泛型和文件操作实例代码

《Go异常处理、泛型和文件操作实例代码》Go语言的异常处理机制与传统的面向对象语言(如Java、C#)所使用的try-catch结构有所不同,它采用了自己独特的设计理念和方法,:本文主要介绍Go异... 目录一:异常处理常见的异常处理向上抛中断程序恢复程序二:泛型泛型函数泛型结构体泛型切片泛型 map三:文

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

SpringSecurity中的跨域问题处理方案

《SpringSecurity中的跨域问题处理方案》本文介绍了跨域资源共享(CORS)技术在JavaEE开发中的应用,详细讲解了CORS的工作原理,包括简单请求和非简单请求的处理方式,本文结合实例代码... 目录1.什么是CORS2.简单请求3.非简单请求4.Spring跨域解决方案4.1.@CrossOr

requests处理token鉴权接口和jsonpath使用方式

《requests处理token鉴权接口和jsonpath使用方式》文章介绍了如何使用requests库进行token鉴权接口的处理,包括登录提取token并保存,还详述了如何使用jsonpath表达... 目录requests处理token鉴权接口和jsonpath使用json数据提取工具总结reques

Python多任务爬虫实现爬取图片和GDP数据

《Python多任务爬虫实现爬取图片和GDP数据》本文主要介绍了基于FastAPI开发Web站点的方法,包括搭建Web服务器、处理图片资源、实现多任务爬虫和数据可视化,同时,还简要介绍了Python爬... 目录一. 基于FastAPI之Web站点开发1. 基于FastAPI搭建Web服务器2. Web服务

C# 空值处理运算符??、?. 及其它常用符号

《C#空值处理运算符??、?.及其它常用符号》本文主要介绍了C#空值处理运算符??、?.及其它常用符号,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录一、核心运算符:直接解决空值问题1.??空合并运算符2.?.空条件运算符二、辅助运算符:扩展空值处理

浅析Python中如何处理Socket超时

《浅析Python中如何处理Socket超时》在网络编程中,Socket是实现网络通信的基础,本文将深入探讨Python中如何处理Socket超时,并提供完整的代码示例和最佳实践,希望对大家有所帮助... 目录开篇引言核心要点逐一深入讲解每个要点1. 设置Socket超时2. 处理超时异常3. 使用sele

SpringMVC配置、映射与参数处理​入门案例详解

《SpringMVC配置、映射与参数处理​入门案例详解》文章介绍了SpringMVC框架的基本概念和使用方法,包括如何配置和编写Controller、设置请求映射规则、使用RestFul风格、获取请求... 目录1.SpringMVC概述2.入门案例①导入相关依赖②配置web.XML③配置SpringMVC

利用Python将PDF文件转换为PNG图片的代码示例

《利用Python将PDF文件转换为PNG图片的代码示例》在日常工作和开发中,我们经常需要处理各种文档格式,PDF作为一种通用且跨平台的文档格式,被广泛应用于合同、报告、电子书等场景,然而,有时我们需... 目录引言为什么选择 python 进行 PDF 转 PNG?Spire.PDF for Python