diff --git a/src/main/java/info/istlab/Zemi01/OCRFrame.java b/src/main/java/info/istlab/Zemi01/OCRFrame.java index 18858f1..51ba939 100644 --- a/src/main/java/info/istlab/Zemi01/OCRFrame.java +++ b/src/main/java/info/istlab/Zemi01/OCRFrame.java @@ -50,9 +50,15 @@ System.out.println(img); // tesseract で画像から文字列を取得する String text = TesseractOCR.getOCRText(img,"out"); - text = text.replace(" ","").replace("\n",""); - System.out.println(text); - new TextEditor(text); + text = text.replace("\n",""); + String result = text.replaceAll( + "(?<=[\\p{IsHan}\\p{IsHiragana}\\p{IsKatakana}\\uFF00-\\uFFEF])\\s(?=[a-zA-Z0-9])", "") + .replaceAll( + "(?<=[a-zA-Z0-9])\\s(?=[\\p{IsHan}\\p{IsHiragana}\\p{IsKatakana}\\uFF00-\\uFFEF])", ""); + result = result.replaceAll( + "(?<=[\\p{IsHan}\\p{IsHiragana}\\p{IsKatakana}\\uFF00-\\uFFEF「」『』【】()[]〈〉《》{}・ー~、。,.])\\s(?=[\\p{IsHan}\\p{IsHiragana}\\p{IsKatakana}\\uFF00-\\uFFEF「」『』【】()[]〈〉《》{}・ー~、。,.])", ""); + System.out.println(result); + new TextEditor(result); } } diff --git a/src/main/java/info/istlab/Zemi01/Say.java b/src/main/java/info/istlab/Zemi01/Say.java new file mode 100644 index 0000000..85c6f83 --- /dev/null +++ b/src/main/java/info/istlab/Zemi01/Say.java @@ -0,0 +1,38 @@ +package info.istlab.Zemi01; + +import java.io.IOException; + +public class Say implements Runnable { + private String text; + Process process; + + public Say(String text) { + this.text = text; + Thread thread = new Thread(this); + thread.start(); + } + + @Override + public void run() { +// say コマンドを実行 + try { + // コマンドを構築 + String command = String.format("say -v Kyoko \"%s\"", text); + + // コマンドを実行 + process = Runtime.getRuntime().exec(command); + + // 実行結果を待つ + process.waitFor(); + System.out.println("読み上げが完了しました。"); + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + } + // 読み上げを停止するメソッド + public void stopSpeaking() { + if (process != null && process.isAlive()) { + process.destroy(); // プロセスを終了 + } + } +} diff --git a/src/main/java/info/istlab/Zemi01/TextEditor.java b/src/main/java/info/istlab/Zemi01/TextEditor.java index 88528f9..e0a7d8a 100644 --- a/src/main/java/info/istlab/Zemi01/TextEditor.java +++ b/src/main/java/info/istlab/Zemi01/TextEditor.java @@ -5,12 +5,15 @@ import javax.swing.JButton; import javax.swing.JFrame; +import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class TextEditor extends JFrame { JTextArea textArea; JButton copyButton; + Say say; + String orig; public TextEditor(String text) { super("TextEditor"); @@ -25,17 +28,56 @@ textArea.setFont(font); textArea.setText(text); + orig = text; textArea.setLineWrap(true); // 折り返し textArea.setEditable(true); // 編集可能; + JButton useKutou = new JButton("句読点を使用"); + useKutou.addActionListener(e -> { + String text2 = textArea.getText(); + orig = text2; + text2 = text2.replace(".", "。").replace(",", "、"); + textArea.setText(text2); + }); + JButton undouseKutou = new JButton("←の実行前に戻す"); + undouseKutou.addActionListener(e -> { + textArea.setText(orig); + }); + copyButton = new JButton("Copy"); copyButton.addActionListener(e -> { textArea.selectAll(); textArea.copy(); }); + JButton speakButton = new JButton("読み上げ開始"); + speakButton.addActionListener(e -> { + String txt = textArea.getSelectedText(); + if (txt.length() == 0) { + txt = textArea.getText(); + } + say = new Say(txt); + }); + JButton speakstopButton = new JButton("読み上げ停止"); + speakstopButton.addActionListener(e -> { + if (say != null) { + say.stopSpeaking(); + } + }); + // JButton partspeakButton = new JButton("選択範囲のみ読み上げ開始"); + // partspeakButton.addActionListener(e -> { + // say = new Say(textArea.getSelectedText()); + // }); + // フレームに追加 add(scrollPane, BorderLayout.CENTER); - add(copyButton, BorderLayout.NORTH); + JPanel northPanel = new JPanel(); + northPanel.add(useKutou); + northPanel.add(undouseKutou); + northPanel.add(copyButton); + northPanel.add(speakButton); + northPanel.add(speakstopButton); + // northPanel.add(partspeakButton); + add(northPanel, BorderLayout.NORTH); setLocationRelativeTo(null); setVisible(true);