java编写抖音上图片转文字的程序

看着有趣就花了点时间,找相关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);
}

});


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