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; } }
时间: 2024-10-08 19:28:31