看着有趣就花了点时间,找相关demo拼凑在一起了。

逻辑是读取图片像素,根据像素RGB值区分深浅,然后再用文字替换,输出成text的文档。最后改变字体到合适的大小。

话不多说,直接贴主代码。为防伸手党,各位添加个包,补个private的命名就好。

/**
* 读取一张图片的RGB值

* @throws Exception
*/
public void getImagePixel(String image) throws Exception {
String text="";
txtExport.creatTxtFile("爱就像蓝天白云");
int[] rgb = new int[3];
File file = new File(image);
BufferedImage bi = null;
try {
bi = ImageIO.read(file);
} catch (Exception e) {
e.printStackTrace();
}
int width = bi.getWidth();
int height = bi.getHeight();
int minx = bi.getMinX();
int miny = bi.getMinY();
System.out.println("width=" + width + ",height=" + height + ".");
System.out.println("minx=" + minx + ",miniy=" + miny + ".");

for (int j = miny; j < height; j++) {
for (int i = minx; i < width; i++) {
int pixel = bi.getRGB(i, j); // 下面三行代码将一个数字转换为RGB数字
rgb[0] = (pixel & 0xff0000) >> 16;
rgb[1] = (pixel & 0xff00) >> 8;
rgb[2] = (pixel & 0xff);

if(rgb[0]*0.299 + rgb[1]*0.578 + rgb[2]*0.114 <= 79){ //深色
text = text +"mm";
}
else if((rgb[0]*0.299 + rgb[1]*0.578 + rgb[2]*0.114 > 79) && (rgb[0]*0.299 + rgb[1]*0.578 + rgb[2]*0.114 <= 158)){ 
text = text +"rr";
}
else if((rgb[0]*0.299 + rgb[1]*0.578 + rgb[2]*0.114 > 158) && (rgb[0]*0.299 + rgb[1]*0.578 + rgb[2]*0.114 <= 237)){  
text = text +"~~";
}
else if(rgb[0]*0.299 + rgb[1]*0.578 + rgb[2]*0.114 > 226){  //浅色
text = text +"^^";
}

}
txtExport.writeTxtFile(text);
text = "";

}

}

/**
* 返回屏幕色彩值

* @param x
* @param y
* @return
* @throws AWTException
*/
public int getScreenPixel(int x, int y) throws AWTException { // 函数返回值为颜色的RGB值。
Robot rb = null; // java.awt.image包中的类,可以用来抓取屏幕,即截屏。
rb = new Robot();
Toolkit tk = Toolkit.getDefaultToolkit(); // 获取缺省工具包
Dimension di = tk.getScreenSize(); // 屏幕尺寸规格
System.out.println(di.width);
System.out.println(di.height);
Rectangle rec = new Rectangle(0, 0, di.width, di.height);
BufferedImage bi = rb.createScreenCapture(rec);
int pixelColor = bi.getRGB(x, y);
 
return 16777216 + pixelColor; // pixelColor的值为负,经过实践得出:加上颜色最大值就是实际颜色值。

}

/**
* 创建文件

* @throws IOException
*/
public static boolean creatTxtFile(String name) throws IOException {
boolean flag = false;
filenameTemp = path + name + ".txt";
File filename = new File(filenameTemp);
if (!filename.exists()) {
filename.createNewFile();
flag = true;
}
return flag;
}
 
/**
* 写文件

* @param newStr
*            新内容
* @throws IOException
*/
@SuppressWarnings("unused")
public static boolean writeTxtFile(String newStr) throws IOException {
// 先读取原有文件内容,然后进行写入操作
boolean flag = false;
String filein = newStr;
String temp = "";
 
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
 
FileOutputStream fos = null;
PrintWriter pw = null;
try {
// 文件路径
File file = new File(filenameTemp);
// 将文件读入输入流
fis = new FileInputStream(file);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
 
// 保存该文件原有的内容
for (int j = 1; (temp = br.readLine()) != null; j++) {
buf = buf.append(temp);
// System.getProperty("line.separator")
// 行与行之间的分隔符 相当于“\n”
buf = buf.append(System.getProperty("line.separator"));
}
buf.append(filein);
 
fos = new FileOutputStream(file);
pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
flag = true;
} catch (IOException e1) {
// TODO 自动生成 catch 块
throw e1;
} finally {
if (pw != null) {
pw.close();
}
if (fos != null) {
fos.close();
}
if (br != null) {
br.close();
}
if (isr != null) {
isr.close();
}
if (fis != null) {
fis.close();
}
}
return flag;
}
  public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Logon window = new Logon();
window.frame.setVisible(true);

} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public Logon() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JButton btnNewButton = new JButton("确认");
btnNewButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
text = textField.getText();
int x = 0;
ReadColorTest rc = new ReadColorTest();
try {
x = rc.getScreenPixel(100, 345);
} catch (AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(x + " - ");
try {
rc.getImagePixel("C:/Users/jin/Desktop/"+text);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
textField.setText(null);
}

});

顺便加了个框图。就可以用了。

java编写抖音上图片转文字的程序相关推荐

  1. java抖音字符视频_抖音上很火的字符画 Android 实现 | 视频转换实现

    最后像国际象棋格子一样,一块一块的替换掉,由于计算相对比较复杂,所以耗时比较长,因此当时那个demo也让我搁置了. 最近看到这篇日推,不由得眼前一亮,因为很少有人在android端做这种东西,因为算法 ...

  2. python 动漫卡通人物图片大全_用Python实现抖音上的“人像动漫化”特效,原来这么简单...

    原标题:用Python实现抖音上的"人像动漫化"特效,原来这么简单 作者 | 黄伟呢 来源 | 数据分析与统计学之美 前几天,女友拉着我和她玩儿抖音,就是这个 人像动漫化的操作,顿 ...

  3. 抖音java表白教程_抖音上的表白代码是什么 抖音表白代码怎么写

    抖音上的表白代码是什么 以前大家总说工科生不懂浪漫,但最近抖音上很流行用代码来表白,将代码编写好,然后将文档发给要表白的对象,然后就可以等着对方进入你的表白套路了.其实这是弹窗表白的代码不是很复杂,一 ...

  4. 抖音爱心c语言程序,抖音上用记事本编写爱心小程序教程

    本文实例为大家分享了抖音上用记事本编写爱心小程序的具体代码,供大家参考,具体内容如下 先上一波效果图: 一颗旋转的小心心 七夕的前一天晚上,当我满怀期待的把这颗"爱心"展示给我女朋 ...

  5. python处理视频动漫化_用Python实现抖音上的“人像动漫化”特效,原来这么简单...

    原标题:用Python实现抖音上的"人像动漫化"特效,原来这么简单 作者 | 黄伟呢 来源 | 数据分析与统计学之美 前几天,女友拉着我和她玩儿抖音,就是这个 人像动漫化的操作,顿 ...

  6. android 字符画,抖音上很火的字符画 Android 实现 | 视频转换实现

    作者:Line_cut_feng 链接:https://www.jianshu.com/p/a14f1ac558e1 之前我推送过抖音上很火的字符画 Android 实现,当时文末:按说拿到ascii ...

  7. python时钟罗盘酷炫代码_抖音上的时钟屏保,被我改造完用来表白

    作者:爱编程的小和尚 原文链接:https://blog.csdn.net/Newbie___/article/details/105378852 抖音上很火的时钟屏保,被我改造完后用来准备准备准备表 ...

  8. cad2016中选择全图字体怎么操作_抖音上的书单都是用啥做的,抖音书单图片怎么做...

    玩抖音的花式玩法相信大家已经见过很多,大概做一下总结可以分为露脸和不露脸的,如果你有表现的欲望,那你就可以考虑做真人出镜. 但如果你不敢面对镜头,有没有也能玩转抖音的方法.最近在抖音上有一种玩法很好, ...

  9. 仿抖音--将图片转化成对应的字符图片

    起初看到抖音上的那个将图片转化成对应的字符图片的视频,心动不已,哇!这哥们也太有心了吧,虽说莉哥好看,但你做这图要花多长时间呀!值不值啊! 后来我才知道,java实现的话简直一秒钟的事. 第一开始只能 ...

最新文章

  1. hitTest和pointInside如何响应用户点击事件
  2. 合并分支到master_我敢打赌!这是全网最全的 Git 分支开发规范手册
  3. 红黑树(一)之 原理和算法详细介绍
  4. 智能化家庭弱电布线标准规范
  5. DAY97 - Rest Framework(二)- 序列化组件之Serializer和ModelSerializer
  6. 项目放到git_Git的基本使用(二)
  7. configure: error: ZLib not installed
  8. UI设计的扁平化图标模板素材
  9. python可视化使用_python可视化pyecharts
  10. linux调度器(二)——CFS模型
  11. Activiti7 + Spring Boot + mybatis Plus + Oracle 数据库整合-学习篇(一)
  12. UWF自定义设置与命令管理
  13. iphone13怎么安装双卡(双卡功能)
  14. 使用FFmpegFrameGrabber获取视频缩略图
  15. 企业如何选择短信平台
  16. 无线串口服务器规模,无线串口服务器讲述.doc
  17. 23 种设计模式详解(全23种)
  18. 查看linux [Fedora] 系统信息
  19. Statistical learning Week 1 什么是统计学习?
  20. python 傅里叶变换_【python学习笔记】21:numpy傅里叶变换

热门文章

  1. 频偏纠正matlab实现,频偏估计matlab代码
  2. 电商系统促销方案的设计
  3. java开发实战经典第二版网课,成功收获美团,小米offer
  4. PLATFORMS(平台)
  5. php 日历排班的例子,基于ThinkPHP实现的日历功能实例详解
  6. 双二极管钳位电路的原理分析
  7. Python数据分析案例10——北向资金流入与沪深300涨跌幅分析
  8. zabbix自定义报警——(邮箱,微信)
  9. html鼠标滑过图片放大效果
  10. It is indirectly referenced from required .class files