您的位置:首页 > 编程语言

一些代码的收集

2015-03-07 10:04 302 查看
FileUtil

import java.io.File;

import java.io.IOException;

import java.util.List;

public class Run {
static String path = "C:/Users/Administrator/Desktop/demo/";
static String genPath = "C:/Users/Administrator/Desktop/demo/in/";
static String targetPath = "C:/Users/Administrator/Desktop/demo/out/";
static String fileName = "QrUtil.java";

public static void main(String[] args) throws Exception {
List<Byte> contents = function1();
function2(contents);
function3();
}

private static void function2(List<Byte> contents) throws Exception {
QrUtil.autoGenQR(contents, new File(genPath), QrUtil.NAME_SET_000);
System.out.println("function 2 is ok");
}

private static List<Byte> function1() throws IOException {
File file = new File(path + fileName);
List<Byte> contens = FileUtil.partFile2StringList(file);
System.out.println("function 1 is ok");
return contens;
}

public static void function3() throws IOException {
File file = new File(targetPath + "\\" + fileName);
List<String> contents = QrUtil.readQrFiles(new File(genPath));
QrUtil.writeFileFromContent(contents, file);
System.out.println("function 3 is ok");
}

}

import java.awt.image.BufferedImage;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Date;

import java.util.Hashtable;

import java.util.List;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;

import com.google.zxing.BinaryBitmap;

import com.google.zxing.DecodeHintType;

import com.google.zxing.EncodeHintType;

import com.google.zxing.LuminanceSource;

import com.google.zxing.MultiFormatReader;

import com.google.zxing.MultiFormatWriter;

import com.google.zxing.NotFoundException;

import com.google.zxing.Result;

import com.google.zxing.WriterException;

import com.google.zxing.common.BitMatrix;

import com.google.zxing.client.j2se.BufferedImageLuminanceSource;

import com.google.zxing.client.j2se.MatrixToImageWriter;

import com.google.zxing.common.HybridBinarizer;

public class QrUtil {
public static String NAME_SET_QR = "qr_";
public static String NAME_SET_000 = "000_";
public static String NAME_SET_DATE = "DATE";
public static String NAME_SET_DATE_TIME = "DATE_TIME";
public static Integer readByte = Integer.parseInt(ReadConfig.getKey("len"));
public static String charSet = "ISO-8859-1";

public static void genQR(String content, File genQrFile) {
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, charSet);
BitMatrix matrix = null;
try {
matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300, hints);//
} catch (WriterException e) {
e.printStackTrace();
}
try {
MatrixToImageWriter.writeToFile(matrix, "png", genQrFile);//syzc
} catch (IOException e) {
e.printStackTrace();
}
}

public static void autoGenQR(List<Byte> contents, File genPath, String nameType) throws Exception {
int fileIndex = 0;
int index = 0;
int maxLength = contents.size();
System.out.println(maxLength);
byte[] buff ;
boolean last_flag = false;
if(maxLength > readByte){
buff = new byte[readByte];
}else{
buff = new byte[maxLength];
File qrFile = new File(genPath + "\\" + getQrFileName(nameType, ++fileIndex) + ".png");
genQR(new String(buff, charSet), qrFile);
return;
}
for (byte b : contents) {
buff[index] = b;
if (index == readByte - 1) {
maxLength -= readByte;
index = -1;
File qrFile = new File(genPath + "\\" + getQrFileName(nameType, ++fileIndex) + ".png");
genQR(new String(buff, charSet), qrFile);

if(maxLength>readByte){
buff = new byte[readByte];
}else if(maxLength >0){
last_flag = true;
buff = new byte[maxLength];
}
}else if(last_flag){
buff[index] = b;
if(--maxLength == 0){
File qrFile = new File(genPath + "\\" + getQrFileName(nameType, ++fileIndex) + ".png");
genQR(new String(buff, charSet), qrFile);
}
}
index++;
}
}

public static void writeFileFromContent(List<String> contents, File writeFile) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(writeFile));
for (String str : contents) {
bos.write(str.getBytes(charSet));
}
bos.flush();
bos.close();
}

public static List<String> readQrFiles(File qrPath) {
List<String> strs = new ArrayList<String>();
File[] files = qrPath.listFiles();
int index = 0;
for (File f : files) {
System.out.println(++index + "," + f.getName());
String content = readQrFile(f);

// System.out.println(Arrays.toString(content.getBytes()));
if (content == null) {
throw new RuntimeException("read file error");
}
strs.add(content);
}
return strs;
}

public static String readQrFile(File file) {
BufferedImage bufferedImage = null;
try {
bufferedImage = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
hints.put(DecodeHintType.CHARACTER_SET, "ISO-8859-1");
try {

Result result = new MultiFormatReader().decode(bitmap, hints);
return result.toString();
} catch (NotFoundException e) {
e.printStackTrace();
}
return null;
}

private static String getQrFileName(String nameType, int index) {
String name = "";
String indexStr = index + "";
for (int i = indexStr.length(); i < 6; i++) {
indexStr = "0" + indexStr;
}
if (nameType.equals(NAME_SET_QR)) {
name = nameType + indexStr;
} else if (nameType.equals(NAME_SET_000)) {
name = nameType + indexStr;
} else if (nameType.equals(NAME_SET_DATE)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
name = sdf.format(new Date()) + "_" + indexStr;
} else if (nameType.equals(NAME_SET_DATE_TIME)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
name = sdf.format(new Date());
} else {
name = nameType + indexStr;
}
return name;
}



public class ReadConfig {
private static Properties prop = null;
static{
try {
InputStream in = new BufferedInputStream(new FileInputStream("config.properties"));
prop = new Properties();
prop.load(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getKey(String key) {
if(prop !=null){
return (String) prop.get(key);
}
return "";
}

}

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.List;

public class FileUtil {
public static Integer readByte = Integer.parseInt(ReadConfig.getKey("len"));
public static String charSet = "ISO-8859-1";

public static List<Byte> partFile2StringList(File file) throws IOException {
List<Byte> bList = new ArrayList<Byte>(); 
FileInputStream bis = new FileInputStream(file);
byte[] buff = new byte[readByte];
int len = 0;
while ((len = bis.read(buff) )> 0) {

// for (byte b : buff) {

// bList.add(b);

// }
for(int i = 0;i<len;i++){
bList.add(buff[i]);
}

// bList.add(new String(buff, 0, len, charSet).getBytes(charsetName));
}
bis.close();
return bList;
}

public static void writeFile(String content, File file) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
if (content == null || content.length() == 0) {
bos.write(content.getBytes(charSet));
bos.flush();
bos.close();
}
}

public static boolean partFile(File file, String target_path) throws IOException {
int len = 0, sumLen = 0, index = 1;
boolean flag = false;
File targetPath = new File(target_path);
FileInputStream fis = new FileInputStream(file);
OutputStream out = new FileOutputStream(target_path + "/" + getFileName(1 + ""));
if (!targetPath.exists()) {
targetPath.mkdir();
}
byte[] buff = new byte[readByte];
while ((len = fis.read(buff)) > 0) {
sumLen += len;
out.write(buff, 0, len);
if (sumLen >= readByte) {
index++;
sumLen = 0;
out.flush();
out.close();
out = new FileOutputStream(target_path + "/" + getFileName(index + ""));
}
}
if (out != null) {
out.flush();
out.close();
}
flag = true;
return flag;
}

public static String getFileName(String index) {
String indexStr = index;
for (int i = indexStr.length(); i < 4; i++) {
indexStr = "0" + indexStr;
}
return indexStr;
}

private static void mergeFiles(List<File> files, String fileName) throws IOException {
File outFile = new File(fileName);
OutputStream out = new FileOutputStream(outFile);
for (File f : files) {
InputStream in = new FileInputStream(f);
int big = 1024;
byte[] buff = new byte[big];
int len = 0;
while ((len = in.read(buff)) > 0) {
out.write(buff, 0, len);
}
in.close();
out.flush();
}
out.close();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  qr