Skip to content

Commit 45a03d2

Browse files
committed
1、添加mp3格式转换工具
1 parent 9e30727 commit 45a03d2

7 files changed

Lines changed: 339 additions & 2 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.xwintop.xJavaFxTool.controller.littleTools;
2+
3+
import com.xwintop.xJavaFxTool.services.littleTools.Mp3ConvertToolService;
4+
import com.xwintop.xJavaFxTool.utils.JavaFxViewUtil;
5+
import com.xwintop.xJavaFxTool.utils.XJavaFxSystemUtil;
6+
import com.xwintop.xJavaFxTool.view.littleTools.Mp3ConvertToolView;
7+
import com.xwintop.xcore.util.javafx.FileChooserUtil;
8+
import javafx.collections.FXCollections;
9+
import javafx.collections.ObservableList;
10+
import javafx.event.ActionEvent;
11+
import javafx.fxml.FXML;
12+
import javafx.scene.control.ContextMenu;
13+
import javafx.scene.control.MenuItem;
14+
import javafx.scene.input.MouseButton;
15+
import lombok.Getter;
16+
import lombok.Setter;
17+
import lombok.extern.slf4j.Slf4j;
18+
import org.apache.commons.io.FileUtils;
19+
import org.apache.commons.lang3.StringUtils;
20+
21+
import java.io.File;
22+
import java.net.URL;
23+
import java.util.Map;
24+
import java.util.ResourceBundle;
25+
26+
/**
27+
* @ClassName: Mp3ConvertToolController
28+
* @Description: mp3格式转换工具
29+
* @author: xufeng
30+
* @date: 2019/8/8 0008 20:41
31+
*/
32+
33+
@Getter
34+
@Setter
35+
@Slf4j
36+
public class Mp3ConvertToolController extends Mp3ConvertToolView {
37+
private Mp3ConvertToolService mp3ConvertToolService = new Mp3ConvertToolService(this);
38+
private ObservableList<Map<String, String>> tableData = FXCollections.observableArrayList();//表格数据
39+
40+
@Override
41+
public void initialize(URL location, ResourceBundle resources) {
42+
initView();
43+
initEvent();
44+
initService();
45+
}
46+
47+
private void initView() {
48+
JavaFxViewUtil.setTableColumnMapValueFactory(fileNameTableColumn, "fileName", false);
49+
JavaFxViewUtil.setTableColumnMapValueFactory(absolutePathTableColumn, "absolutePath", false);
50+
JavaFxViewUtil.setTableColumnMapValueFactory(fileSizeTableColumn, "fileSize", false);
51+
JavaFxViewUtil.setTableColumnMapValueFactory(convertStatusTableColumn, "convertStatus", false);
52+
tableViewMain.setItems(tableData);
53+
}
54+
55+
private void initEvent() {
56+
FileChooserUtil.setOnDrag(outputFolderTextField, FileChooserUtil.FileType.FOLDER);
57+
tableViewMain.setOnMouseClicked(event -> {
58+
if (event.getButton() == MouseButton.SECONDARY) {
59+
MenuItem menu_Remove = new MenuItem("移除选中行");
60+
menu_Remove.setOnAction(event1 -> {
61+
tableData.remove(tableViewMain.getSelectionModel().getSelectedItem());
62+
});
63+
MenuItem menu_RemoveAll = new MenuItem("移除所有");
64+
menu_RemoveAll.setOnAction(event1 -> {
65+
tableData.clear();
66+
});
67+
tableViewMain.setContextMenu(new ContextMenu(menu_Remove, menu_RemoveAll));
68+
}
69+
});
70+
}
71+
72+
private void initService() {
73+
}
74+
75+
@FXML
76+
private void addFileAction(ActionEvent event) throws Exception {
77+
File file = FileChooserUtil.chooseFile();
78+
if (file != null) {
79+
mp3ConvertToolService.addTableData(file);
80+
}
81+
}
82+
83+
@FXML
84+
private void addFolderAction(ActionEvent event) throws Exception {
85+
File folder = FileChooserUtil.chooseDirectory();
86+
if (folder != null) {
87+
FileUtils.listFiles(folder, null, false).forEach((File file) -> {
88+
mp3ConvertToolService.addTableData(file);
89+
});
90+
}
91+
}
92+
93+
@FXML
94+
private void convertAction(ActionEvent event) {
95+
mp3ConvertToolService.convertAction();
96+
}
97+
98+
@FXML
99+
private void outputFolderAction(ActionEvent event) {
100+
File folder = FileChooserUtil.chooseDirectory();
101+
if (folder != null) {
102+
outputFolderTextField.setText(folder.getPath());
103+
}
104+
}
105+
106+
@FXML
107+
private void openOutputFolderAction(ActionEvent event) {
108+
if (StringUtils.isNotEmpty(outputFolderTextField.getText())) {
109+
XJavaFxSystemUtil.openDirectory(outputFolderTextField.getText());
110+
}
111+
}
112+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.xwintop.xJavaFxTool.services.littleTools;
2+
3+
import com.xwintop.xJavaFxTool.controller.littleTools.Mp3ConvertToolController;
4+
import com.xwintop.xcore.util.FileUtil;
5+
import lombok.Getter;
6+
import lombok.Setter;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.apache.commons.io.FileUtils;
9+
import org.apache.commons.lang3.StringUtils;
10+
11+
import java.io.File;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
/**
16+
* @ClassName: Mp3ConvertToolService
17+
* @Description: mp3格式转换工具
18+
* @author: xufeng
19+
* @date: 2019/8/8 0008 20:41
20+
*/
21+
22+
@Getter
23+
@Setter
24+
@Slf4j
25+
public class Mp3ConvertToolService {
26+
private Mp3ConvertToolController mp3ConvertToolController;
27+
28+
public Mp3ConvertToolService(Mp3ConvertToolController mp3ConvertToolController) {
29+
this.mp3ConvertToolController = mp3ConvertToolController;
30+
}
31+
32+
public void addTableData(File file) {
33+
Map<String, String> rowValue = new HashMap<>();
34+
rowValue.put("fileName", file.getName());
35+
rowValue.put("absolutePath", file.getAbsolutePath());
36+
rowValue.put("fileSize", FileUtil.formetFileSize(file.length()));
37+
rowValue.put("convertStatus", "待转换");
38+
mp3ConvertToolController.getTableData().add(rowValue);
39+
}
40+
41+
public void convertAction() {
42+
for (Map<String, String> tableDatum : mp3ConvertToolController.getTableData()) {
43+
String absolutePath = tableDatum.get("absolutePath");
44+
if (StringUtils.endsWithIgnoreCase(absolutePath, ".qmcflac")) {
45+
if (convertQmc(absolutePath)) {
46+
tableDatum.put("convertStatus", "转换成功");
47+
} else {
48+
tableDatum.put("convertStatus", "转换失败");
49+
}
50+
}
51+
}
52+
mp3ConvertToolController.getTableViewMain().refresh();
53+
}
54+
55+
private boolean convertQmc(String absolutePath) {
56+
try {
57+
byte[] buffer = FileUtils.readFileToByteArray(new File(absolutePath));
58+
QmcDecode dc = new QmcDecode();
59+
for (int i = 0; i < buffer.length; ++i) {
60+
buffer[i] = (byte) (dc.NextMask() ^ buffer[i]);
61+
}
62+
String file = StringUtils.removeEndIgnoreCase(absolutePath, ".qmcflac");
63+
if (StringUtils.isEmpty(mp3ConvertToolController.getOutputFolderTextField().getText())) {
64+
FileUtils.writeByteArrayToFile(new File(file + ".mp3"), buffer);
65+
} else {
66+
FileUtils.writeByteArrayToFile(new File(mp3ConvertToolController.getOutputFolderTextField().getText(), new File(file + ".mp3").getName()), buffer);
67+
}
68+
return true;
69+
} catch (Exception e) {
70+
log.error("转换异常", e);
71+
return false;
72+
}
73+
}
74+
}
75+
76+
class QmcDecode {
77+
private int x = -1;
78+
private int y = 8;
79+
private int dx = 1;
80+
private int index = -1;
81+
private int[][] seedMap = {
82+
{0x4a, 0xd6, 0xca, 0x90, 0x67, 0xf7, 0x52},
83+
{0x5e, 0x95, 0x23, 0x9f, 0x13, 0x11, 0x7e},
84+
{0x47, 0x74, 0x3d, 0x90, 0xaa, 0x3f, 0x51},
85+
{0xc6, 0x09, 0xd5, 0x9f, 0xfa, 0x66, 0xf9},
86+
{0xf3, 0xd6, 0xa1, 0x90, 0xa0, 0xf7, 0xf0},
87+
{0x1d, 0x95, 0xde, 0x9f, 0x84, 0x11, 0xf4},
88+
{0x0e, 0x74, 0xbb, 0x90, 0xbc, 0x3f, 0x92},
89+
{0x00, 0x09, 0x5b, 0x9f, 0x62, 0x66, 0xa1}
90+
};
91+
92+
public int NextMask() {
93+
int ret;
94+
index++;
95+
if (x < 0) {
96+
dx = 1;
97+
y = ((8 - y) % 8);
98+
ret = ((8 - y) % 8);
99+
ret = 0xc3;
100+
} else if (x > 6) {
101+
dx = -1;
102+
y = 7 - y;
103+
ret = 0xd8;
104+
} else {
105+
ret = seedMap[y][x];
106+
}
107+
108+
x += dx;
109+
if (index == 0x8000 || (index > 0x8000 && (index + 1) % 0x8000 == 0)) {
110+
return NextMask();
111+
}
112+
return ret;
113+
}
114+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.xwintop.xJavaFxTool.view.littleTools;
2+
3+
import javafx.fxml.FXML;
4+
import javafx.fxml.Initializable;
5+
import javafx.scene.control.Button;
6+
import javafx.scene.control.TableColumn;
7+
import javafx.scene.control.TableView;
8+
import javafx.scene.control.TextField;
9+
import lombok.Getter;
10+
import lombok.Setter;
11+
12+
import java.util.Map;
13+
14+
/**
15+
* @ClassName: Mp3ConvertToolView
16+
* @Description: mp3格式转换工具
17+
* @author: xufeng
18+
* @date: 2019/8/8 0008 20:41
19+
*/
20+
21+
@Getter
22+
@Setter
23+
public abstract class Mp3ConvertToolView implements Initializable {
24+
@FXML
25+
protected TableView<Map<String, String>> tableViewMain;
26+
@FXML
27+
protected TableColumn<Map<String, String>, String> fileNameTableColumn;
28+
@FXML
29+
protected TableColumn<Map<String, String>, String> absolutePathTableColumn;
30+
@FXML
31+
protected TableColumn<Map<String, String>, String> fileSizeTableColumn;
32+
@FXML
33+
protected TableColumn<Map<String, String>, String> convertStatusTableColumn;
34+
@FXML
35+
protected Button addFileButton;
36+
@FXML
37+
protected Button addFolderButton;
38+
@FXML
39+
protected Button convertButton;
40+
@FXML
41+
protected TextField outputFolderTextField;
42+
@FXML
43+
protected Button outputFolderButton;
44+
@FXML
45+
protected Button openOutputFolderButton;
46+
47+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<?import javafx.geometry.Insets?>
4+
<?import javafx.scene.control.Button?>
5+
<?import javafx.scene.control.Label?>
6+
<?import javafx.scene.control.TableColumn?>
7+
<?import javafx.scene.control.TableView?>
8+
<?import javafx.scene.control.TextField?>
9+
<?import javafx.scene.layout.AnchorPane?>
10+
<?import javafx.scene.layout.BorderPane?>
11+
<?import javafx.scene.layout.HBox?>
12+
13+
<AnchorPane prefHeight="521.0" prefWidth="755.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.xwintop.xJavaFxTool.controller.littleTools.Mp3ConvertToolController">
14+
<children>
15+
<BorderPane layoutX="54.0" layoutY="49.0" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="10.0">
16+
<center>
17+
<TableView fx:id="tableViewMain" BorderPane.alignment="CENTER">
18+
<columns>
19+
<TableColumn fx:id="fileNameTableColumn" minWidth="100.0" prefWidth="150.0" text="文件名称" />
20+
<TableColumn fx:id="absolutePathTableColumn" minWidth="200.0" prefWidth="300.0" text="完整路径" />
21+
<TableColumn fx:id="fileSizeTableColumn" maxWidth="200.0" minWidth="50.0" prefWidth="100.0" text="大小" />
22+
<TableColumn fx:id="convertStatusTableColumn" maxWidth="200.0" minWidth="50.0" prefWidth="100.0" text="状态" />
23+
</columns>
24+
<BorderPane.margin>
25+
<Insets bottom="10.0" top="10.0" />
26+
</BorderPane.margin>
27+
<columnResizePolicy>
28+
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
29+
</columnResizePolicy>
30+
</TableView>
31+
</center>
32+
<bottom>
33+
<HBox alignment="CENTER" spacing="5.0" BorderPane.alignment="CENTER">
34+
<children>
35+
<Button fx:id="addFileButton" mnemonicParsing="false" onAction="#addFileAction" text="添加文件" />
36+
<Button fx:id="addFolderButton" mnemonicParsing="false" onAction="#addFolderAction" text="添加文件夹" />
37+
<Button fx:id="convertButton" mnemonicParsing="false" onAction="#convertAction" text="一键转换" />
38+
<Label text="输出文件夹:">
39+
<HBox.margin>
40+
<Insets left="10.0" right="-10.0" />
41+
</HBox.margin>
42+
</Label>
43+
<TextField fx:id="outputFolderTextField" promptText="默认为原目录" />
44+
<Button fx:id="outputFolderButton" mnemonicParsing="false" onAction="#outputFolderAction" text="选择" />
45+
<Button fx:id="openOutputFolderButton" mnemonicParsing="false" onAction="#openOutputFolderAction" text="打开输出文件夹" />
46+
</children>
47+
<BorderPane.margin>
48+
<Insets />
49+
</BorderPane.margin>
50+
</HBox>
51+
</bottom>
52+
<top>
53+
<Label text="目前支持.ncm、.qmc转换为mp3格式" BorderPane.alignment="CENTER" />
54+
</top>
55+
</BorderPane>
56+
</children>
57+
</AnchorPane>

src/main/resources/config/toolFxmlLoaderConfiguration.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@
289289
<url>/com/xwintop/xJavaFxTool/fxmlView/littleTools/FileSearchTool.fxml</url>
290290
<title>FileSearchTool</title>
291291
<menuParentId>littleTools</menuParentId>
292+
</ToolFxmlLoaderConfiguration>
293+
<ToolFxmlLoaderConfiguration>
294+
<url>/com/xwintop/xJavaFxTool/fxmlView/littleTools/Mp3ConvertTool.fxml</url>
295+
<title>Mp3ConvertTool</title>
296+
<menuParentId>littleTools</menuParentId>
292297
<isDefaultShow>true</isDefaultShow>
293298
</ToolFxmlLoaderConfiguration>
294299

src/main/resources/locale/Menu.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,5 @@ FileMergeTool=\u6587\u4EF6\u5408\u5E76\u5DE5\u5177
8888
SedentaryReminderTool=\u4E45\u5750\u63D0\u9192\u5DE5\u5177
8989
RandomGeneratorTool=\u968F\u673A\u6570\u751F\u6210\u5DE5\u5177
9090
ClipboardHistoryTool=\u526A\u8D34\u677F\u5386\u53F2\u5DE5\u5177
91-
FileSearchTool=\u6587\u4EF6\u641C\u7D22\u5DE5\u5177
91+
FileSearchTool=\u6587\u4EF6\u641C\u7D22\u5DE5\u5177
92+
Mp3ConvertTool=Mp3\u8F6C\u6362\u5DE5\u5177

src/main/resources/locale/Menu_en_US.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,5 @@ FileMergeTool=FileMergeTool
8888
SedentaryReminderTool=SedentaryReminderTool
8989
RandomGeneratorTool=RandomGeneratorTool
9090
ClipboardHistoryTool=ClipboardHistoryTool
91-
FileSearchTool=FileSearchTool
91+
FileSearchTool=FileSearchTool
92+
Mp3ConvertTool=Mp3ConvertTool

0 commit comments

Comments
 (0)