package cc.mrbird.febs.mall.test; import cn.hutool.core.util.ObjectUtil; import com.madgag.gif.fmsware.AnimatedGifEncoder; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; /** * 基于Java的图片字符画(含动图) * https://blog.csdn.net/weixin_53070478/article/details/127183821?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_utm_term~default-4-127183821-blog-125549681.235^v43^pc_blog_bottom_relevance_base9&spm=1001.2101.3001.4242.3&utm_relevant_index=7 */ public class ImageToChar { static char[] ss = " `.^,:~\"> 16 & 0xff; rgb[1] = pixel >> 8 & 0xff; rgb[2] = pixel & 0xff; int Gray = (rgb[0] * 28 + rgb[1] * 151 + rgb[2] * 77) >> 8; //通过三原色值计算像素点的灰度 int x = Gray * ss.length / 255 % ss.length; //灰度值的百分比 计算出相应密度的字符表 bw.write(ss[x]); //输出该字符 bw.write(ss[x]); } bw.newLine(); } bw.flush(); } catch (IOException e) { e.printStackTrace(); } return bw; } /** * GIF转图片 * https://blog.csdn.net/adminstate/article/details/128917090?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522171653969716800197097670%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=171653969716800197097670&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-128917090-null-null.142^v100^pc_search_result_base8&utm_term=java%20%E5%9B%BE%E7%89%87%E8%BD%ACGif&spm=1018.2226.3001.4187 * @param args */ // public static void main(String[] args) { // Integer scaleSize = 360; // GifDecoder gd = new GifDecoder(); // try{ // gd.read(Files.newInputStream(new File("D:\\image\\imageGif.gif").toPath())); // for (int i = 0; i < gd.getFrameCount(); i++) { //逐帧转换为图片。 // BufferedImage frame = gd.getFrame(i); // float width = frame.getWidth(); // 像素 // float height = frame.getHeight(); // 像素 // float scale = width/scaleSize; // BufferedImage buffImg = new BufferedImage(scaleSize, (int)(height/scale), BufferedImage.TYPE_INT_RGB); // //使用TYPE_INT_RGB修改的图片会变色 // buffImg.getGraphics().drawImage( // frame.getScaledInstance(scaleSize, (int)(height/scale), Image.SCALE_SMOOTH), 0, // 0, null); // // ImageIO.write(buffImg, "jpg", new File("D:\\image\\gif\\"+i+".png")); // } // }catch(IOException e){ // System.out.println(e); // } // } /** * 图片转GIF * https://blog.csdn.net/adminstate/article/details/128917090?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522171653969716800197097670%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=171653969716800197097670&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-128917090-null-null.142^v100^pc_search_result_base8&utm_term=java%20%E5%9B%BE%E7%89%87%E8%BD%ACGif&spm=1018.2226.3001.4187 * @param args */ public static void main(String[] args) { // 存放jpg文件目录 String inpath = "D:\\image\\gif\\"; //生成GIF AnimatedGifEncoder animatedGifEncoder = new AnimatedGifEncoder(); animatedGifEncoder.setRepeat(0); animatedGifEncoder.start("D:\\image\\gif\\screenshot.gif"); try{ //获取目录下所有jpg及其他非gif图片文件 String[] pic = new File(inpath).list(); BufferedImage[] src = new BufferedImage[pic.length]; for (int i = 0; i <= src.length; i++) { String pngPath = inpath + i + ".png"; if(new File(pngPath).exists()){ animatedGifEncoder.setDelay(100); //设置播放间隔 src[i] = ImageIO.read(new File(pngPath)); // 读入需要播放的jpg文件 animatedGifEncoder.addFrame(src[i]); //添加到帧中 } } animatedGifEncoder.finish(); }catch(IOException e){ System.err.println("合成gif图失败"); } } /** * 将GIF图片转换成字符串 * @param args */ // public static void main(String[] args) { // GifDecoder gd = new GifDecoder(); // try{ // // gd.read(Files.newInputStream(new File("D:\\image\\gif\\screenshot.gif").toPath())); // System.out.println(gd.getFrameCount()); // for (int i = 0; i < gd.getFrameCount(); i++) { //逐帧转换为字符集。 // System.out.println(gd.getFrameCount()); // BufferedImage frame = gd.getFrame(i); // ImageToChar(frame,"D:\\image\\ImageToChar.txt"); //参数提供缓冲图片对象,以及目标文本输出地址。 // try { // Thread.sleep(500); // } catch (InterruptedException e) { // e.printStackTrace(); // } // } // }catch(IOException e){ // System.out.println(e); // } // } /** * 将图片转换成字符串 * @param args */ // public static void main(String[] args) { // try{ // ImageToChar( // ImageIO.read(new File("D:\\image\\outDpi.png")), // "D:\\image\\ImageToChar.txt"); // }catch(IOException e){ // System.out.println(e); // } // } }