java 实现十六进制数与图片的相互转换
最编程
2024-05-04 22:28:52
...
武汉源创会回归,4月20聊聊大模型”
1、十六进制转图片jpg
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Map;
/**
* 十六进制转成图片
* @author Administrator
*
*/
public class Hex2Image {
public static void main(String[] args) throws Exception {
Hex2Image to=new Hex2Image();
InputStream is=new FileInputStream("f://aa.txt");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String str = null;
StringBuilder sb = new StringBuilder();
while ((str = br.readLine()) != null) {
System.out.println(str);
sb.append(str);
}
to.saveToImgFile(sb.toString().toUpperCase(),"f://dd.jpg");
}
public void saveToImgFile(String src,String output){
if(src==null||src.length()==0){
return;
}
try{
FileOutputStream out = new FileOutputStream(new File(output));
byte[] bytes = src.getBytes();
for(int i=0;i<bytes.length;i+=2){
out.write(charToInt(bytes[i])*16+charToInt(bytes[i+1]));
}
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
private int charToInt(byte ch){
int val = 0;
if(ch>=0x30&&ch<=0x39){
val=ch-0x30;
}else if(ch>=0x41&&ch<=0x46){
val=ch-0x41+10;
}
return val;
}
}
2.图片转成16进制
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
/**
* 图片转成十六进制
* @author Administrator
*
*/
public class ImageToHex {
public static void main(String[] args) throws Exception {
try{
StringBuffer sb = new StringBuffer();
FileInputStream fis = new FileInputStream("f:/345.jpg");
BufferedInputStream bis = new BufferedInputStream(fis);
java.io.ByteArrayOutputStream bos=new java.io.ByteArrayOutputStream();
byte[] buff=new byte[1024];
int len=0;
while((len=fis.read(buff))!=-1){
bos.write(buff,0,len);
}
//得到图片的字节数组
byte[] result=bos.toByteArray();
System.out.println("++++"+byte2HexStr(result));
//字节数组转成十六进制
String str=byte2HexStr(result);
/*
* 将十六进制串保存到txt文件中
*/
PrintWriter pw = new PrintWriter(new FileWriter("f://today.txt"));
pw.println(str);
pw.close();
}catch(IOException e){
}
}
/*
* 实现字节数组向十六进制的转换方法一
*/
public static String byte2HexStr(byte[] b) {
String hs="";
String stmp="";
for (int n=0;n<b.length;n++) {
stmp=(Integer.toHexString(b[n] & 0XFF));
if (stmp.length()==1) hs=hs+"0"+stmp;
else hs=hs+stmp;
}
return hs.toUpperCase();
}
private static byte uniteBytes(String src0, String src1) {
byte b0 = Byte.decode("0x" + src0).byteValue();
b0 = (byte) (b0 << 4);
byte b1 = Byte.decode("0x" + src1).byteValue();
byte ret = (byte) (b0 | b1);
return ret;
}
/*
*实现字节数组向十六进制的转换的方法二
*/
public static String bytesToHexString(byte[] src){
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
}
推荐阅读
-
java 实现十六进制数与图片的相互转换
-
一种结构设计模式,允许在对象中动态添加新行为。它通过创建一个封装器来实现这一目的,即把对象放入一个装饰器类中,然后把这个装饰器类放入另一个装饰器类中,以此类推,形成一个封装器链。这样,我们就可以在不改变原始对象的情况下动态添加新行为或修改原始行为。 在 Java 中,实现装饰器设计模式的步骤如下: 定义一个接口或抽象类作为被装饰对象的基类。 公共接口 Component { void operation; } } 在本例中,我们定义了一个名为 Component 的接口,该接口包含一个名为 operation 的抽象方法,该方法定义了被装饰对象的基本行为。 定义一个实现基类方法的具体装饰对象。 公共类 ConcreteComponent 实现 Component { public class ConcreteComponent implements Component { @Override public void operation { System.out.println("ConcreteComponent is doing something...") ; } } 定义一个抽象装饰器类,该类继承于基类,并将装饰对象作为一个属性。 公共抽象类装饰器实现组件 { protected Component 组件 public Decorator(Component component) { this.component = component; } } @Override public void operation { component.operation; } } } 在这个示例中,我们定义了一个名为 Decorator 的抽象类,它继承了 Component 接口,并将被装饰对象作为一个属性。在操作方法中,我们调用了被装饰对象上的同名方法。 定义一个具体的装饰器类,继承自抽象装饰器类并实现增强逻辑。 公共类 ConcreteDecoratorA extends Decorator { public ConcreteDecoratorA(Component 组件) { super(component); } } public void operation { super.operation System.out.println("ConcreteDecoratorA 正在添加新行为......") ; } } 在本例中,我们定义了一个名为 ConcreteDecoratorA 的具体装饰器类,它继承自装饰器抽象类,并实现了操作方法的增强逻辑。在操作方法中,我们首先调用被装饰对象上的同名方法,然后添加新行为。 使用装饰器增强被装饰对象。 公共类 Main { public static void main(String args) { Component 组件 = new ConcreteComponent; component = new ConcreteDecoratorA(component); 组件操作 } } 在这个示例中,我们首先创建了一个被装饰对象 ConcreteComponent,然后通过 ConcreteDecoratorA 类创建了一个装饰器,并将被装饰对象作为参数传递。最后,调用装饰器的操作方法,实现对被装饰对象的增强。 使用场景 在 Java 中,装饰器模式被广泛使用,尤其是在 I/O 中。Java 中的 I/O 库使用装饰器模式实现了不同数据流之间的转换和增强。 让我们打开文件 a.txt,从中读取数据。InputStream 是一个抽象类,FileInputStream 是专门用于读取文件流的子类。BufferedInputStream 是一个支持缓存的数据读取类,可以提高数据读取的效率,具体代码如下: @Test public void testIO throws Exception { InputStream inputStream = new FileInputStream("C:/bbb/a.txt"); // 实现包装 inputStream = new BufferedInputStream(inputStream); byte bytes = new byte[1024]; int len; while((len = inputStream.read(bytes)) != -1){ System.out.println(new String(bytes, 0, len)); } } } } 其中 BufferedInputStream 对读取数据进行了增强。 这样看来,装饰器设计模式和代理模式似乎有点相似,接下来让我们讨论一下它们之间的区别。 第三,与代理模式的区别: 代理模式的目的是控制对对象的访问,它在对象外部提供一个代理对象来控制对原对象的访问。代理对象和原始对象通常实现相同的接口或继承相同的类,以确保两者可以相互替换。 装饰器模式的目的是动态增强对象的功能,而这是通过对象内部的包装器来实现的。在装饰器模式中,装饰器类和被装饰对象通常实现相同的接口或继承自相同的类,以确保两者可以相互替代。装饰器模式也被称为封装器模式。 在代理模式中,代理类附加了与原类无关的功能。
-
Java Json 与 List、Map、entity 的相互转换
-
Java 将实现对象与 Map 的相互转换--使用 BeanMap
-
图解指南:二进制、八进制与十六进制相互转换详解——(四) 八进制到十六进制的转化实例
-
在Java中轻松实现JSONObject和Json字符串之间的相互转换
-
在Java中,如何快速实现XML到JSON的转换? - 解析与生成指南:关键步骤包括解析XML、构造数据模型、转化为JSON。讲解如下:首先,利用JDOM库读懂XML文档,提取元素和属性;接着,基于这些信息设计适当的数据结构(如树形或网状结构);最终,运用Jackson库将数据结构顺利转存为JSON格式。同时,我们也讨论了何时选用JDOM和Jackson这类工具辅助转换过程。
-
在Java中轻松实现图片与二进制数据之间的相互转换
-
在Java中实现数字与字母的互相转换
-
JavaScript实现中文数字与阿拉伯数字的相互转换