FileDepart
package march.io;
import java.io.*;
public class FileDepart {
public static void main(String[] args) throws Exception{
String sourceFile = "C:\\Users\\Administrator\\Desktop\\xpath\\run.zip";
String descDir = "C:\\Users\\Administrator\\Desktop\\xpath\\runDepart";
int blockSize = 512;
byte[] fileData = readFile(sourceFile);
int eIndex = 0;int sIndex = 0;int fileName = 0;
while (eIndex!=fileData.length-1){
fileName++;
eIndex = fileName*blockSize-1;
if(eIndex>fileData.length-1){
eIndex=fileData.length-1;
}
byte[] outputData = new byte[eIndex-sIndex+1];
for(int i=0;i<outputData.length;i++){
outputData[i] = fileData[sIndex+i];
}
System.out.println((fileData.length-1)+" "+eIndex);
writeFileBlock(descDir,fileName,outputData);
sIndex= eIndex+1;
}
}
public static byte[] readFile(String fileName) throws Exception{
FileInputStream fileInputStream = new FileInputStream(fileName);
DataInputStream dataInputStream = new DataInputStream(fileInputStream);
byte[] res = new byte[dataInputStream.available()];
dataInputStream.readFully(res);
fileInputStream.close();
return res;
}
public static void writeFileBlock(String savePath,int blockName,byte[] data) throws Exception{
String fileString = blockName+" "+bytesToHexString(data);
File file = new File(savePath+"/"+blockName+".txt");
if(!file.exists()){
file.createNewFile();
}
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(fileString);
bufferedWriter.close();
}
public static String bytesToHexString(byte[] src){
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
}
QRShow
package march.io;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class QRShow extends JFrame {
private String _sourcePath;
private int _qrCodeSizeW = 800;
private int _qrCodeSizeH = 800;
JTextField textField = new JTextField("0",20);
ImagePanel imagePanel = null;
public static void main(String[] args){
String sourcePath ="C:\\Users\\Administrator\\Desktop\\xpath\\runDepart";
int qrCodeSizeW = 1000;
int qrCodeSizeH = 1000;
QRShow q = new QRShow(sourcePath,qrCodeSizeW,qrCodeSizeH);
}
public QRShow(String sourcePath,int qrCodeSizeW,int qrCodeSizeH){
this._sourcePath = sourcePath;
this._qrCodeSizeW = qrCodeSizeW;
this._qrCodeSizeH = qrCodeSizeH;
imagePanel = new ImagePanel(this);
init();
this.setSize(new Dimension(800,600));
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void init(){
this.setLayout(new BorderLayout(10,5));
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(20,1));
final JButton btnNext = new JButton("->");
final JButton btnPre = new JButton("<-");
panel.add(textField);
panel.add(btnPre);
panel.add(btnNext);
this.add(panel,BorderLayout.EAST);
this.add(imagePanel,BorderLayout.CENTER);
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent event) {
if(event.getID()==KeyEvent.KEY_PRESSED){
switch (((KeyEvent)event).getKeyCode()) {
case KeyEvent.VK_RIGHT:
btnNext.doClick();
break;
case KeyEvent.VK_LEFT:
btnPre.doClick();
break;
}
}
}
}, AWTEvent.KEY_EVENT_MASK);
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showQr(true);
}
});
btnPre.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showQr(false);
}
});
}
public void showQr(boolean next){
try{
int blockName = Integer.parseInt(this.textField.getText());
blockName = next?(blockName+1):(blockName-1);
imagePanel.displayBlock(blockName);
this.textField.setText(blockName+"");
}catch (Exception e){
//e.printStackTrace();
}
}
public class ImagePanel extends JPanel{
public Image image;
private QRShow _qrShow;
ImagePanel(QRShow qrShow){
_qrShow = qrShow;
}
public void displayBlock(int blockName){
String filePath = _sourcePath+"/"+blockName+".txt";
File file = new File(filePath);
if(file.exists()){
try {
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String dataString = bufferedReader.readLine();
bufferedReader.close();
this.image = getBarCode(dataString);
this.repaint();
_qrShow.setTitle(blockName+"");
} catch (IOException e) {
e.printStackTrace();
}
}else{
_qrShow.setTitle("-------");
}
}
public Image getBarCode(String fileString){
try {
Map<EncodeHintType,String> map =new HashMap<EncodeHintType, String>();
map.put(EncodeHintType.CHARACTER_SET,"UTF-8");
map.put(EncodeHintType.MARGIN,"0");
BitMatrix bitMatrix = new MultiFormatWriter().encode(fileString, BarcodeFormat.QR_CODE,_qrCodeSizeW,_qrCodeSizeH,map);
BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
return bufferedImage;
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void paint(Graphics graphics){
super.paint(graphics);
graphics.drawImage(image,0,0,this.getSize().height,this.getSize().height,null);
}
}
}
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>march.io</groupId>
<artifactId>march-qrcode</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
</project>