1. 程式人生 > >java編寫抖音上圖片轉文字的程式

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);
}

});


順便加了個框圖。就可以用了。