首页 > 代码库 > Java中图片文件和字符串的相互转化
Java中图片文件和字符串的相互转化
import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import javax.imageio.ImageIO; public class TypeConverter { private static String ImageFileFormat = "jpg"; private static StringBuffer sb1 = null; private static BufferedImage buffImage = null; private static ByteArrayOutputStream baos = null; private static byte[] byteImage = null; private static String ImageFileNameFormat = "yyyy-MM-dd_HH-mm-ss"; private static String splitSymbol = ","; private static String[] imageStringArr = null; // picturePath为图片文件的在磁盘上的保存路径,返回值为文件转换成字符串之后的字符串,转换过程以逗号分隔(此方法理论上也可以将非图片格式的文件转换成字符串) public static String ImageFileToString(String picturePath) { sb1 = new StringBuffer(); try { buffImage = ImageIO.read(new File(picturePath)); baos = new ByteArrayOutputStream(); ImageIO.write(buffImage, ImageFileFormat, baos); } catch (IOException e) { e.printStackTrace(); } byteImage = baos.toByteArray(); for (int i = 0; i < byteImage.length; i++) { if (sb1.length() == 0) { sb1.append(byteImage[i]); } else { sb1.append(splitSymbol + byteImage[i]); } } return sb1.toString(); } // pictureString为需要转换成图片文件的字符串,pictureToPath是字符串转换为图片文件之后的图片文件的保存路径 public static void StringToImageFile(String pictureString, String pictureToPath) { if (pictureString.contains(",")) { imageStringArr = split(pictureString, splitSymbol); byte[] image = new byte[imageStringArr.length]; for (int i = 0; i < imageStringArr.length; i++) { image[i] = Byte.parseByte(imageStringArr[i]); } String name = saveImage(image, pictureToPath); } else { System.out.println("(字符串----->图片文件)不能解析的格式!"); } } private static String[] split(String s, String token) { if (s == null) return null; if (token == null || s.length() == 0) return new String[] { s }; int size = 0; String[] result = new String[4]; while (s.length() > 0) { int index = s.indexOf(token); String splitOne = s; if (index > -1) { splitOne = s.substring(0, index); s = s.substring(index + token.length()); } else { s = ""; } if (size >= result.length) { String[] tmp = new String[result.length * 2]; System.arraycopy(result, 0, tmp, 0, result.length); result = tmp; } if (splitOne.length() > 0) { result[size++] = splitOne; } } String[] tmp = result; result = new String[size]; System.arraycopy(tmp, 0, result, 0, size); return result; } private static BufferedImage readImage(byte[] bytes) throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(bytes); return ImageIO.read(bais); } private static String saveImage(byte[] imgages, final String saveDir) { try { BufferedImage bis = readImage(imgages); DateFormat sdf = new SimpleDateFormat(ImageFileNameFormat); String fileTime = sdf.format(new Date()); final String name = fileTime + "_" + "." + ImageFileFormat; File f = new File(saveDir + name); boolean istrue = false; if (f.exists()) { istrue = ImageIO.write(bis, ImageFileFormat, f); } else { f.mkdirs(); istrue = ImageIO.write(bis, ImageFileFormat, f); } if (istrue) { return name; } } catch (Exception e) { } return null; } }
Java中图片文件和字符串的相互转化
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。