how to print image to thermal printer in kotlin - image
i already able to print text using kotlin to thermal printer, but im still dont know how to print image to thermal printer in kotlin. please give me sample printing image to thermal printer in kotlin.i already search for the topics, but it written in java, i doesnt know java very much thanks for the help
private fun p1() {
val namaToko = "Rose Medical"
val alamatToko = "Pramuka raya no.1 Jakarta Timur"
val telp = "021-85901642"
val enter = "\n"
val strip = "-"
val rp ="Rp."
val ex = " X "
val textTotal = "Total Rp:"
val ppnTv = "PPN :Rp."
val chargeTv ="Charge :Rp."
val totalTv = "Total Belanja :Rp."
val scope = CoroutineScope(Dispatchers.IO)
scope.launch {
// chunks1
try{
writeWithFormat(namaToko.toByteArray(),Formatter().get(),Formatter.centerAlign())
writeWithFormat(enter.toByteArray(),Formatter().get(),Formatter.leftAlign())
writeWithFormat(alamatToko.toByteArray(),Formatter().get(),Formatter.centerAlign())
writeWithFormat(enter.toByteArray(),Formatter().get(),Formatter.leftAlign())
writeWithFormat(telp.toByteArray(),Formatter().get(),Formatter.centerAlign())
writeWithFormat(enter.toByteArray(),Formatter().get(),Formatter.leftAlign())
writeWithFormat(enter.toByteArray(),Formatter().get(),Formatter.rightAlign())
}catch (e: Exception) {
Log.e("PrintActivity", "Exe ", e)
}
// chunks2
for(pointer in salesBody.indices){
try {
val merk = salesBody[pointer].merk
writeWithFormat(merk!!.toByteArray(),Formatter().get(),Formatter.leftAlign())
writeWithFormat(strip.toByteArray(),Formatter().get(),Formatter.leftAlign())
val barang = salesBody[pointer].namaBrg
writeWithFormat(barang!!.toByteArray(),Formatter().get(),Formatter.leftAlign())
writeWithFormat(enter.toByteArray(),Formatter().get(),Formatter.leftAlign())
val varian = salesBody[pointer].varian
writeWithFormat(varian!!.toByteArray(),Formatter().get(),Formatter.leftAlign())
writeWithFormat(enter.toByteArray(),Formatter().get(),Formatter.leftAlign())
writeWithFormat(rp.toByteArray(),Formatter().get(),Formatter.leftAlign())
val harga = ValidNumber().deciformat(salesBody[pointer].hargaJual.toString())
writeWithFormat(harga.toByteArray(),Formatter().get(),Formatter.leftAlign())
writeWithFormat(ex.toByteArray(),Formatter().get(),Formatter.leftAlign())
val jumlah = ValidNumber().deciformat(salesBody[pointer].qty.toString())
writeWithFormat(jumlah.toByteArray(),Formatter().get(),Formatter.leftAlign())
val satuan = salesBody[pointer].unit
writeWithFormat(satuan!!.toByteArray(),Formatter().get(),Formatter.leftAlign())
writeWithFormat(enter.toByteArray(),Formatter().get(),Formatter.leftAlign())
writeWithFormat(textTotal.toByteArray(),Formatter().get(),Formatter.rightAlign())
val total = ValidNumber().deciformat(salesBody[pointer].total.toString())
writeWithFormat(total.toByteArray(),Formatter().get(),Formatter.leftAlign())
writeWithFormat(enter.toByteArray(),Formatter().get(),Formatter.leftAlign())
}catch (e: Exception) {
Log.e("PrintActivity", "Exe ", e)
}
}
// chunks3
try{
writeWithFormat(enter.toByteArray(),Formatter().get(),Formatter.leftAlign())
val tanggal = salesHeader[0].tanggal
writeWithFormat(tanggal!!.toByteArray(),Formatter().get(),Formatter.leftAlign())
writeWithFormat(strip.toByteArray(),Formatter().get(),Formatter.leftAlign())
val jam = salesHeader[0].jam
writeWithFormat(jam!!.toByteArray(),Formatter().get(),Formatter.leftAlign())
writeWithFormat(strip.toByteArray(),Formatter().get(),Formatter.leftAlign())
val idTag= salesHeader[0].idTag
writeWithFormat(idTag!!.toByteArray(),Formatter().get(),Formatter.leftAlign())
writeWithFormat(enter.toByteArray(),Formatter().get(),Formatter.leftAlign())
val payment= salesHeader[0].payment
writeWithFormat(payment!!.toByteArray(),Formatter().get(),Formatter.leftAlign())
writeWithFormat(enter.toByteArray(),Formatter().get(),Formatter.leftAlign())
writeWithFormat(ppnTv.toByteArray(),Formatter().get(),Formatter.rightAlign())
val ppnValue = ValidNumber().deciformat(salesHeader[0].ppn.toString())
writeWithFormat(ppnValue.toByteArray(),Formatter().get(),Formatter.rightAlign())
writeWithFormat(enter.toByteArray(),Formatter().get(),Formatter.rightAlign())
writeWithFormat(chargeTv.toByteArray(),Formatter().get(),Formatter.rightAlign())
val chargeValue = ValidNumber().deciformat(salesHeader[0].charge.toString())
writeWithFormat(chargeValue.toByteArray(),Formatter().get(),Formatter.rightAlign())
writeWithFormat(enter.toByteArray(),Formatter().get(),Formatter.rightAlign())
writeWithFormat(totalTv.toByteArray(),Formatter().get(),Formatter.rightAlign())
var totalValue = ValidNumber().deciformat(salesHeader[0].allTotal.toString())
writeWithFormat(totalValue.toByteArray(),Formatter().get(),Formatter.rightAlign())
writeWithFormat(enter.toByteArray(),Formatter().get(),Formatter.rightAlign())
writeWithFormat(enter.toByteArray(),Formatter().get(),Formatter.rightAlign())
writeWithFormat(enter.toByteArray(),Formatter().get(),Formatter.rightAlign())
writeWithFormat(enter.toByteArray(),Formatter().get(),Formatter.rightAlign())
writeWithFormat(enter.toByteArray(),Formatter().get(),Formatter.rightAlign())
writeWithFormat(enter.toByteArray(),Formatter().get(),Formatter.rightAlign())
}catch (e: Exception) {
Log.e("PrintActivity", "Exe ", e)
}
}
}
//print code
class Formatter {
/** The format that is being build on */
private val mFormat: ByteArray
init {
// Default:
mFormat = byteArrayOf(27, 33, 0)
}
/**
* Method to get the Build result
*
* #return the format
*/
fun get(): ByteArray {
return mFormat
}
fun bold(): Formatter {
// Apply bold:
mFormat[2] = (0x8 or mFormat[2].toInt()).toByte()
return this
}
fun small(): Formatter {
mFormat[2] = (0x1 or mFormat[2].toInt()).toByte()
return this
}
fun height(): Formatter {
mFormat[2] = (0x10 or mFormat[2].toInt()).toByte()
return this
}
fun width(): Formatter {
mFormat[2] = (0x20 or mFormat[2].toInt()).toByte()
return this
}
fun underlined(): Formatter {
mFormat[2] = (0x80 or mFormat[2].toInt()).toByte()
return this
}
companion object {
fun rightAlign(): ByteArray {
return byteArrayOf(0x1B, 'a'.code.toByte(), 0x02)
}
fun leftAlign(): ByteArray {
return byteArrayOf(0x1B, 'a'.code.toByte(), 0x00)
}
fun centerAlign(): ByteArray {
return byteArrayOf(0x1B, 'a'.code.toByte(), 0x01)
}
}
}//last
fun writeWithFormat(buffer: ByteArray, pFormat: ByteArray?, pAlignment: ByteArray?): Boolean {
val mmOutStream: OutputStream = mBluetoothSocket.outputStream
return try {
// Notify printer it should be printed with given alignment:
mmOutStream.write(pAlignment)
// Notify printer it should be printed in the given format:
mmOutStream.write(pFormat)
// Write the actual data:
mmOutStream.write(buffer, 0, buffer.size)
// Share the sent message back to the UI Activity
//App.getInstance().getHandler().obtainMessage(MESSAGE_WRITE, buffer.size, -1, buffer).sendToTarget()
true
} catch (e: IOException) {
Log.e(TAG, "Exception during write", e)
false
}
}
//print code close
Using Thermal Printer repo references, I managed to print image to Bluetooth thermal printer. Make sure to use a proper black and white image to print.
Here is the piece of code I used to make a java class
--BitmapHelper.java.
public class BitmapHelper {
private static String[] binaryArray = { "0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111" };
public static byte[] decodeBitmap(Bitmap bmp){
//Bitmap bmp = Bitmap.createScaledBitmap(bitmap, 50, 50, false);
int maxWidth = 350;
int bmpWidth = bmp.getWidth();
int bmpHeight = bmp.getHeight();
if(bmpWidth > maxWidth){
float aspectRatio = bmp.getWidth() /
(float) bmp.getHeight();
bmpWidth = maxWidth;
bmpHeight = Math.round(bmpWidth / aspectRatio);
bmp = Bitmap.createScaledBitmap(bmp, bmpWidth, bmpHeight, false);
}
List<String> list = new ArrayList<>(); //binaryString list
StringBuffer sb;
int zeroCount = bmpWidth % 8;
StringBuilder zeroStr = new StringBuilder();
if (zeroCount > 0) {
for (int i = 0; i < (8 - zeroCount); i++) {
zeroStr.append("0");
}
}
for (int i = 0; i < bmpHeight; i++) {
sb = new StringBuffer();
for (int j = 0; j < bmpWidth; j++) {
int color = bmp.getPixel(j, i);
int r = (color >> 16) & 0xff;
int g = (color >> 8) & 0xff;
int b = color & 0xff;
// if color close to white,bit='0', else bit='1'
if (r > 160 && g > 160 && b > 160)
sb.append("0");
else
sb.append("1");
}
if (zeroCount > 0) {
sb.append(zeroStr);
}
list.add(sb.toString());
}
List<String> bmpHexList = binaryListToHexStringList(list);
String commandHexString = "1D763000";
String widthHexString = Integer
.toHexString(bmpWidth % 8 == 0 ? bmpWidth / 8
: (bmpWidth / 8 + 1));
if (widthHexString.length() > 2) {
Log.e("decodeBitmap error", " width is too large");
return null;
} else if (widthHexString.length() == 1) {
widthHexString = "0" + widthHexString;
}
widthHexString = widthHexString + "00";
String heightHexString = Integer.toHexString(bmpHeight);
if (heightHexString.length() > 2) {
Log.e("decodeBitmap error", " height is too large");
return null;
} else if (heightHexString.length() == 1) {
heightHexString = "0" + heightHexString;
}
heightHexString = heightHexString + "00";
List<String> commandList = new ArrayList<>();
commandList.add(commandHexString+widthHexString+heightHexString);
commandList.addAll(bmpHexList);
return hexList2Byte(commandList);
}
private static List<String> binaryListToHexStringList(List<String> list) {
List<String> hexList = new ArrayList<String>();
for (String binaryStr : list) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < binaryStr.length(); i += 8) {
String str = binaryStr.substring(i, i + 8);
String hexString = myBinaryStrToHexString(str);
sb.append(hexString);
}
hexList.add(sb.toString());
}
return hexList;
}
private static String myBinaryStrToHexString(String binaryStr) {
StringBuilder hex = new StringBuilder();
String f4 = binaryStr.substring(0, 4);
String b4 = binaryStr.substring(4, 8);
String hexStr = "0123456789ABCDEF";
for (int i = 0; i < binaryArray.length; i++) {
if (f4.equals(binaryArray[i]))
hex.append(hexStr.substring(i, i + 1));
}
for (int i = 0; i < binaryArray.length; i++) {
if (b4.equals(binaryArray[i]))
hex.append(hexStr.substring(i, i + 1));
}
return hex.toString();
}
private static byte[] hexList2Byte(List<String> list) {
List<byte[]> commandList = new ArrayList<byte[]>();
for (String hexStr : list) {
commandList.add(hexStringToBytes(hexStr));
}
return sysCopy(commandList);
}
private 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[] sysCopy(List<byte[]> srcArrays) {
int len = 0;
for (byte[] srcArray : srcArrays) {
len += srcArray.length;
}
byte[] destArray = new byte[len];
int destLen = 0;
for (byte[] srcArray : srcArrays) {
System.arraycopy(srcArray, 0, destArray, destLen, srcArray.length);
destLen += srcArray.length;
}
return destArray;
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
}
Then I use it in the print activity:
private fun p2(){
val convertBmp : Bitmap
convertBmp = BitmapFactory.decodeResource(getResources(),com.example.ronibluetooth.R.drawable.poly)
val decodeBmp = BitmapHelper.decodeBitmap(convertBmp)
val scope = CoroutineScope(Dispatchers.IO)
scope.launch {
try {
val os =mBluetoothSocket.outputStream
os.write(decodeBmp,0,decodeBmp.size)
}
catch (e: Exception) {
Log.e("PrintActivity", "Exe ", e)
}
}
}
image to print
result print
Related
Spring Boot example how to download file from server
I have a Spring Boot web application and I need an example of how to download a file from the server. Thanks, R.
I have this running project of SpringBoot. Below is a part of code which outputs xlsx file. WebController.java #RequestMapping(value = {"/excel"}, method = RequestMethod.GET) public void excel(HttpServletResponse response, #RequestParam("email") String email) { try { Query query = new Query(); query.addCriteria(Criteria.where("email").is(email)); if(email.equals("")) query=new Query(); List<MQTT_Server_Detail> list = mongoTemplate.find(query, MQTT_Server_Detail.class, "owner"); response.addHeader("Content-disposition", "attachment; filename=Door.xlsx"); response.setContentType("application/vnd.ms-excel"); Workbook workbook = new XSSFWorkbook(); workbook.createSheet("owner"); workbook.setSheetName(0, "Owner"); Sheet sheet = workbook.getSheetAt(0); sheet.createRow(0); sheet.getRow(0).createCell(0).setCellValue("Owner Email"); sheet.getRow(0).createCell(1).setCellValue("Topic"); sheet.getRow(0).createCell(2).setCellValue("Device Name"); sheet.getRow(0).createCell(3).setCellValue("Device ID"); Row row; int num = 1; for (MQTT_Server_Detail a : list) { Devices devices = getDevice(a.getEmail()); for (int i = 0; i < devices.getDevicesID().size(); i++) { row = sheet.createRow(num++); row.createCell(0).setCellValue(a.getEmail()); row.createCell(1).setCellValue(a.getTopic()); row.createCell(2).setCellValue((String) devices.getDevicesName().get(i)); row.createCell(3).setCellValue((String) devices.getDevicesID().get(i)); } } sheet = workbook.createSheet("Users"); row = sheet.createRow(0); row.createCell(0).setCellValue("Name"); row.createCell(1).setCellValue("Device"); row.createCell(2).setCellValue("CardID"); row.createCell(3).setCellValue("Email"); row.createCell(4).setCellValue("Mobile"); row.createCell(5).setCellValue("Blocked"); row.createCell(6).setCellValue("Last in Date"); row.createCell(7).setCellValue("Last in Time"); row.createCell(8).setCellValue("Last out Date"); row.createCell(9).setCellValue("Last out Time"); row.createCell(10).setCellValue("Owner"); Criteria criteria[] = new Criteria[list.size()]; for (int i = 0; i < list.size(); i++) { criteria[i] = Criteria.where("owner").is(list.get(i).getEmail()); } List<Users_POJO> users_pojoList; if (list.size() == 0) users_pojoList = new ArrayList<>(); else users_pojoList = mongoTemplate.find(new Query().addCriteria(new Criteria().orOperator(criteria)), Users_POJO.class, "users"); for (int i = 0; i < users_pojoList.size(); i++) { row = sheet.createRow(i + 1); row.createCell(0).setCellValue(users_pojoList.get(i).getName()); row.createCell(1).setCellValue(users_pojoList.get(i).getDevice()); row.createCell(2).setCellValue(users_pojoList.get(i).getCard_id()); row.createCell(3).setCellValue(users_pojoList.get(i).getEmail()); row.createCell(4).setCellValue(users_pojoList.get(i).getMobile()); row.createCell(5).setCellValue(users_pojoList.get(i).getBlocked()); row.createCell(6).setCellValue(users_pojoList.get(i).getLast_in_date()); row.createCell(7).setCellValue(users_pojoList.get(i).getLast_in_time()); row.createCell(8).setCellValue(users_pojoList.get(i).getLast_out_date()); row.createCell(9).setCellValue(users_pojoList.get(i).getLast_out_time()); row.createCell(10).setCellValue(users_pojoList.get(i).getOwner()); } sheet = workbook.createSheet("Logs"); row = sheet.createRow(0); row.createCell(0).setCellValue("CardID"); row.createCell(1).setCellValue("Device"); row.createCell(2).setCellValue("Date"); row.createCell(3).setCellValue("Time"); row.createCell(4).setCellValue("Count"); criteria = new Criteria[users_pojoList.size()]; for (int i = 0; i < users_pojoList.size(); i++) { criteria[i] = Criteria.where("card_id").is(users_pojoList.get(i).getCard_id()); } query = new Query(); query.addCriteria(new Criteria().orOperator(criteria)); List<Log_POJO> log_pojoList; if (users_pojoList.size() == 0) log_pojoList = new ArrayList<>(); else log_pojoList = mongoTemplate.find(query, Log_POJO.class, "logs"); for (int i = 0; i < log_pojoList.size(); i++) { row = sheet.createRow(i + 1); row.createCell(0).setCellValue(log_pojoList.get(i).getCard_id()); row.createCell(1).setCellValue(log_pojoList.get(i).getDevice()); String date = log_pojoList.get(i).getDay() + "-" + log_pojoList.get(i).getMonth() + "-" + log_pojoList.get(i).getYear(); row.createCell(2).setCellValue(date); String time = log_pojoList.get(i).getHour() + "-" + log_pojoList.get(i).getMin() + "-" + log_pojoList.get(i).getSec(); row.createCell(3).setCellValue(time); row.createCell(4).setCellValue(log_pojoList.get(i).getSerial_no()); } workbook.write(response.getOutputStream()); response.flushBuffer(); } catch (Exception e) { e.printStackTrace(); } } In short, you have to do this: response.addHeader("Content-disposition", "attachment; filename=Door.xlsx"); response.setContentType("application/vnd.ms-excel"); //get the outputstream of response and write data to it
Spring Boot download file from server #Controller #RequestMapping(value = "/") public class HomeController { #RequestMapping(value = "/download", method = RequestMethod.GET) public StreamingResponseBody getSteamingFile(HttpServletResponse response) throws IOException { response.setContentType("text/html;charset=UTF-8"); response.setHeader("Content-Disposition", "attachment; filename=\"webpage.html\""); InputStream inputStream = new FileInputStream(new File("C:\\MyWebPage\\webpage.html")); return outputStream -> { int nRead; byte[] data = new byte[1024]; while ((nRead = inputStream.read(data, 0, data.length)) != -1) { outputStream.write(data, 0, nRead); } inputStream.close(); }; } }
java.lang.NumberFormatException when reading text from keyboard
This program reads numbers from keyboard and sums it until user writes "total", but then I get a java.lang.NumberFormatException. Boolean isTotal = false; int sum = 0; while(!isTotal) { java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); String s = br.readLine(); if(s=="total") { isTotal = true; } if(!isTotal) sum = sum + Integer.parseInt(s); } System.out.println(sum);
Boolean isTotal = false; int sum = 0; while(!isTotal) { java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); String s = br.readLine(); if(s.equals("total")) { isTotal = true; } if(!isTotal) sum = sum + Integer.parseInt(s); } System.out.println(sum);
How to add tags to a parsed tree that has no tag?
For example, the parsing tree from Stanford Sentiment Treebank "(2 (2 (2 near) (2 (2 the) (2 end))) (3 (3 (2 takes) (2 (2 on) (2 (2 a) (2 (2 whole) (2 (2 other) (2 meaning)))))) (2 .)))", where the number is the sentiment label of each node. I want to add POS tagging information to each node. Such as: "(NP (ADJP (IN near)) (DT the) (NN end)) " I have tried to directly parse the sentence, but the resulted tree is different from that in the Sentiment Treebank (may be because of the parsing version or parameters, I have tried to contact to the author but there is no response). How can I obtain the tagging information?
I think the code in edu.stanford.nlp.sentiment.BuildBinarizedDataset should be helpful. The main() method steps through how these binary trees can be created in Java code. Some key lines to look out for in the code: LexicalizedParser parser = LexicalizedParser.loadModel(parserModel); TreeBinarizer binarizer = TreeBinarizer.simpleTreeBinarizer(parser.getTLPParams().headFinder(), parser.treebankLanguagePack()); ... Tree tree = parser.apply(tokens); Tree binarized = binarizer.transformTree(tree); You can access the node tag information from the Tree object. You should look at the javadoc for edu.stanford.nlp.trees.Tree to see how to access this information. Also in this answer I have some code that shows accessing a Tree: How to get NN andNNS from a text? You want to look at the label() of each tree and subtree to get the tag for a node. Here is the reference on GitHub to BuildBinarizedDataset.java: https://github.com/stanfordnlp/CoreNLP/blob/master/src/edu/stanford/nlp/sentiment/BuildBinarizedDataset.java Please let me know if anything is unclear about this and I can provide further assistance!
First, you need to download the Stanford Parser Set up private LexicalizedParser parser; private TreeBinarizer binarizer; private CollapseUnaryTransformer transformer; parser = LexicalizedParser.loadModel(PCFG_PATH); binarizer = TreeBinarizer.simpleTreeBinarizer( parser.getTLPParams().headFinder(), parser.treebankLanguagePack()); transformer = new CollapseUnaryTransformer(); Parse Tree tree = parser.apply(tokens); Access POSTAG public String[] constTreePOSTAG(Tree tree) { Tree binarized = binarizer.transformTree(tree); Tree collapsedUnary = transformer.transformTree(binarized); Trees.convertToCoreLabels(collapsedUnary); collapsedUnary.indexSpans(); List<Tree> leaves = collapsedUnary.getLeaves(); int size = collapsedUnary.size() - leaves.size(); String[] tags = new String[size]; HashMap<Integer, Integer> index = new HashMap<Integer, Integer>(); int idx = leaves.size(); int leafIdx = 0; for (Tree leaf : leaves) { Tree cur = leaf.parent(collapsedUnary); // go to preterminal int curIdx = leafIdx++; boolean done = false; while (!done) { Tree parent = cur.parent(collapsedUnary); if (parent == null) { tags[curIdx] = cur.label().toString(); break; } int parentIdx; int parentNumber = parent.nodeNumber(collapsedUnary); if (!index.containsKey(parentNumber)) { parentIdx = idx++; index.put(parentNumber, parentIdx); } else { parentIdx = index.get(parentNumber); done = true; } tags[curIdx] = parent.label().toString(); cur = parent; curIdx = parentIdx; } } return tags; } Here is the full source code ConstituencyParse.java that run: Use param: java ConstituencyParse -tokpath outputtoken.toks -parentpath outputparent.txt -tagpath outputag.txt < input_sentence_in_text_file_one_sent_per_line.txt (Note: the source code is adapt from treelstm repo, you also need to replace preprocess-sst.py to call ConstituencyParse.java file below) import edu.stanford.nlp.process.WordTokenFactory; import edu.stanford.nlp.ling.HasWord; import edu.stanford.nlp.ling.Word; import edu.stanford.nlp.ling.CoreLabel; import edu.stanford.nlp.ling.TaggedWord; import edu.stanford.nlp.process.PTBTokenizer; import edu.stanford.nlp.util.StringUtils; import edu.stanford.nlp.parser.lexparser.LexicalizedParser; import edu.stanford.nlp.parser.lexparser.TreeBinarizer; import edu.stanford.nlp.tagger.maxent.MaxentTagger; import edu.stanford.nlp.trees.GrammaticalStructure; import edu.stanford.nlp.trees.GrammaticalStructureFactory; import edu.stanford.nlp.trees.PennTreebankLanguagePack; import edu.stanford.nlp.trees.Tree; import edu.stanford.nlp.trees.Trees; import edu.stanford.nlp.trees.TreebankLanguagePack; import edu.stanford.nlp.trees.TypedDependency; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.StringReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.HashMap; import java.util.Properties; import java.util.Scanner; public class ConstituencyParse { private boolean tokenize; private BufferedWriter tokWriter, parentWriter, tagWriter; private LexicalizedParser parser; private TreeBinarizer binarizer; private CollapseUnaryTransformer transformer; private GrammaticalStructureFactory gsf; private static final String PCFG_PATH = "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz"; public ConstituencyParse(String tokPath, String parentPath, String tagPath, boolean tokenize) throws IOException { this.tokenize = tokenize; if (tokPath != null) { tokWriter = new BufferedWriter(new FileWriter(tokPath)); } parentWriter = new BufferedWriter(new FileWriter(parentPath)); tagWriter = new BufferedWriter(new FileWriter(tagPath)); parser = LexicalizedParser.loadModel(PCFG_PATH); binarizer = TreeBinarizer.simpleTreeBinarizer( parser.getTLPParams().headFinder(), parser.treebankLanguagePack()); transformer = new CollapseUnaryTransformer(); // set up to produce dependency representations from constituency trees TreebankLanguagePack tlp = new PennTreebankLanguagePack(); gsf = tlp.grammaticalStructureFactory(); } public List<HasWord> sentenceToTokens(String line) { List<HasWord> tokens = new ArrayList<>(); if (tokenize) { PTBTokenizer<Word> tokenizer = new PTBTokenizer(new StringReader(line), new WordTokenFactory(), ""); for (Word label; tokenizer.hasNext(); ) { tokens.add(tokenizer.next()); } } else { for (String word : line.split(" ")) { tokens.add(new Word(word)); } } return tokens; } public Tree parse(List<HasWord> tokens) { Tree tree = parser.apply(tokens); return tree; } public String[] constTreePOSTAG(Tree tree) { Tree binarized = binarizer.transformTree(tree); Tree collapsedUnary = transformer.transformTree(binarized); Trees.convertToCoreLabels(collapsedUnary); collapsedUnary.indexSpans(); List<Tree> leaves = collapsedUnary.getLeaves(); int size = collapsedUnary.size() - leaves.size(); String[] tags = new String[size]; HashMap<Integer, Integer> index = new HashMap<Integer, Integer>(); int idx = leaves.size(); int leafIdx = 0; for (Tree leaf : leaves) { Tree cur = leaf.parent(collapsedUnary); // go to preterminal int curIdx = leafIdx++; boolean done = false; while (!done) { Tree parent = cur.parent(collapsedUnary); if (parent == null) { tags[curIdx] = cur.label().toString(); break; } int parentIdx; int parentNumber = parent.nodeNumber(collapsedUnary); if (!index.containsKey(parentNumber)) { parentIdx = idx++; index.put(parentNumber, parentIdx); } else { parentIdx = index.get(parentNumber); done = true; } tags[curIdx] = parent.label().toString(); cur = parent; curIdx = parentIdx; } } return tags; } public int[] constTreeParents(Tree tree) { Tree binarized = binarizer.transformTree(tree); Tree collapsedUnary = transformer.transformTree(binarized); Trees.convertToCoreLabels(collapsedUnary); collapsedUnary.indexSpans(); List<Tree> leaves = collapsedUnary.getLeaves(); int size = collapsedUnary.size() - leaves.size(); int[] parents = new int[size]; HashMap<Integer, Integer> index = new HashMap<Integer, Integer>(); int idx = leaves.size(); int leafIdx = 0; for (Tree leaf : leaves) { Tree cur = leaf.parent(collapsedUnary); // go to preterminal int curIdx = leafIdx++; boolean done = false; while (!done) { Tree parent = cur.parent(collapsedUnary); if (parent == null) { parents[curIdx] = 0; break; } int parentIdx; int parentNumber = parent.nodeNumber(collapsedUnary); if (!index.containsKey(parentNumber)) { parentIdx = idx++; index.put(parentNumber, parentIdx); } else { parentIdx = index.get(parentNumber); done = true; } parents[curIdx] = parentIdx + 1; cur = parent; curIdx = parentIdx; } } return parents; } // convert constituency parse to a dependency representation and return the // parent pointer representation of the tree public int[] depTreeParents(Tree tree, List<HasWord> tokens) { GrammaticalStructure gs = gsf.newGrammaticalStructure(tree); Collection<TypedDependency> tdl = gs.typedDependencies(); int len = tokens.size(); int[] parents = new int[len]; for (int i = 0; i < len; i++) { // if a node has a parent of -1 at the end of parsing, then the node // has no parent. parents[i] = -1; } for (TypedDependency td : tdl) { // let root have index 0 int child = td.dep().index(); int parent = td.gov().index(); parents[child - 1] = parent; } return parents; } public void printTokens(List<HasWord> tokens) throws IOException { int len = tokens.size(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < len - 1; i++) { if (tokenize) { sb.append(PTBTokenizer.ptbToken2Text(tokens.get(i).word())); } else { sb.append(tokens.get(i).word()); } sb.append(' '); } if (tokenize) { sb.append(PTBTokenizer.ptbToken2Text(tokens.get(len - 1).word())); } else { sb.append(tokens.get(len - 1).word()); } sb.append('\n'); tokWriter.write(sb.toString()); } public void printParents(int[] parents) throws IOException { StringBuilder sb = new StringBuilder(); int size = parents.length; for (int i = 0; i < size - 1; i++) { sb.append(parents[i]); sb.append(' '); } sb.append(parents[size - 1]); sb.append('\n'); parentWriter.write(sb.toString()); } public void printTags(String[] tags) throws IOException { StringBuilder sb = new StringBuilder(); int size = tags.length; for (int i = 0; i < size - 1; i++) { sb.append(tags[i]); sb.append(' '); } sb.append(tags[size - 1]); sb.append('\n'); tagWriter.write(sb.toString().toLowerCase()); } public void close() throws IOException { if (tokWriter != null) tokWriter.close(); parentWriter.close(); tagWriter.close(); } public static void main(String[] args) throws Exception { String TAGGER_MODEL = "stanford-tagger/models/english-left3words-distsim.tagger"; Properties props = StringUtils.argsToProperties(args); if (!props.containsKey("parentpath")) { System.err.println( "usage: java ConstituencyParse -deps - -tokenize - -tokpath <tokpath> -parentpath <parentpath>"); System.exit(1); } // whether to tokenize input sentences boolean tokenize = false; if (props.containsKey("tokenize")) { tokenize = true; } // whether to produce dependency trees from the constituency parse boolean deps = false; if (props.containsKey("deps")) { deps = true; } String tokPath = props.containsKey("tokpath") ? props.getProperty("tokpath") : null; String parentPath = props.getProperty("parentpath"); String tagPath = props.getProperty("tagpath"); ConstituencyParse processor = new ConstituencyParse(tokPath, parentPath, tagPath, tokenize); Scanner stdin = new Scanner(System.in); int count = 0; long start = System.currentTimeMillis(); while (stdin.hasNextLine() && count < 2) { String line = stdin.nextLine(); List<HasWord> tokens = processor.sentenceToTokens(line); //end tagger Tree parse = processor.parse(tokens); // produce parent pointer representation int[] parents = deps ? processor.depTreeParents(parse, tokens) : processor.constTreeParents(parse); String[] tags = processor.constTreePOSTAG(parse); // print if (tokPath != null) { processor.printTokens(tokens); } processor.printParents(parents); processor.printTags(tags); // print tag StringBuilder sb = new StringBuilder(); int size = tags.length; for (int i = 0; i < size - 1; i++) { sb.append(tags[i]); sb.append(' '); } sb.append(tags[size - 1]); sb.append('\n'); count++; if (count % 100 == 0) { double elapsed = (System.currentTimeMillis() - start) / 1000.0; System.err.printf("Parsed %d lines (%.2fs)\n", count, elapsed); } } long totalTimeMillis = System.currentTimeMillis() - start; System.err.printf("Done: %d lines in %.2fs (%.1fms per line)\n", count, totalTimeMillis / 100.0, totalTimeMillis / (double) count); processor.close(); } }
j2me midlet chinese character display message garbled
My J2ME midlet could retrieves message in Chinese character from a PHP server but it's garbled. The server basically returns the response string and by detecting the first 2 characters. AA = good, anything else indicates error of which the message is to be passed to the calling function for display InputStream is = null; StringBuffer sb = null; String str = ""; HttpConnection http = null; DataOutputStream dos = null; try { URL = login.getURL(); URL += ctlFunction + "/" + uriStr; URL = EncodeURL(URL); //System.out.println(URL); if(!ctlFunction.equals("login")) { msg += "&user=" + login.getUsername(); msg += "&token=" + login.getToken(); } msg += "&lang=" + System.getProperty("microedition.locale"); // establish the connection http = (HttpConnection) Connector.open(URL); http.setRequestMethod(HttpConnection.POST); http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); http.setRequestProperty("Content-length", ""+EncodeURL(msg).getBytes().length); dos = http.openDataOutputStream(); byte[] request_body = EncodeURL(msg).getBytes(); for (int i = 0; i < request_body.length; i++) { dos.writeByte(request_body[i]); } // server response if (http.getResponseCode() == HttpConnection.HTTP_OK) { is = http.openDataInputStream(); int length = (int) http.getLength(); if (length != -1) { // Read data in one chunk byte serverData[] = new byte[length]; is.read(serverData); str = new String(serverData); } else // Length not available... { ByteArrayOutputStream bStrm = new ByteArrayOutputStream(); int ch; while ((ch = is.read()) != -1) bStrm.write(ch); str = new String(bStrm.toByteArray()); bStrm.close(); } } else { networkError(); } } catch (Exception e) { System.err.println("Error3: " + e.toString()); networkError(e.toString()); } finally { if (is != null) is.close(); if (!str.equals("")) post = str; else networkError(); if (http != null) http.close(); } if (post != null) { String fate = post.substring(0, 2); if(fate.equals("AA")) { if(ctlFunction.equals("login")) { String rawPost = post.substring(2); Vector v = new Vector(); int index = 0; //find the first occurrence of the SPLITTER int endIndex = rawPost.indexOf(SPLITTER, index); String item = ""; //extract the items until the end of the last SPLITTER found in the rawPost string while(endIndex != -1) { item = rawPost.substring(index, endIndex); index = endIndex + 1; endIndex = rawPost.indexOf(SPLITTER, index); v.addElement(item); } //extract the rest of the rawPost (the text item) item = rawPost.substring(index); v.addElement(item); String[] ret = new String[v.size()]; v.copyInto(ret); login.setToken(ret[0]); login.setToday(ret[1]); login.setNextDrawDay(ret[2]); } midlet.returnResults(post.substring(2), getCurrentDisplay(), ctlFunction); } else { String errmessage = post.substring(2); System.out.println(post); midlet.showInfo(post, getCurrentDisplay()); } } else { networkError(); } On the PHP server, I have set the header to UTF-8 encoding <?php header("Content-Type:text/plain; charset=utf-8"); ?> What could possibly be wrong?
I found that this user has the same problem and it's been answered Reading UTF8 strings from a server through http using MIDP. Kudos to the answer. I basically edited my MIDP code from // is = http.openDataInputStream(); // int length = (int) http.getLength(); // if (length != -1) // { // // Read data in one chunk // byte serverData[] = new byte[length]; // is.read(serverData); // str = new String(serverData); // } // else // Length not available... // { // ByteArrayOutputStream bStrm = new ByteArrayOutputStream(); // int ch; // while ((ch = is.read()) != -1) // bStrm.write(ch); // // str = new String(bStrm.toByteArray()); // bStrm.close(); // } to Reader r = new InputStreamReader(http.openInputStream(), "UTF-8"); int ch; while((ch = r.read()) != -1) str = str + (char)ch; just wondering though why does reading bytes messes up the UTF-8 characters?
split one big datatable to two separated datatables
I´m exporting datatables to Excel workbook. Problem is that the datatable holds 90000 rows and excel can only hold 67000 rows in every sheet. So.. How can i divide one big datatable to two datatables, maybe with Linq ? Then i can have datatable1 in sheet1 and datatable2 in sheet2 Sincerly agh
Assuming that you're getting the 90,000 rows for this DataTable from a database somewhere, the most efficient approach would be to modify your SELECT statement into two new SELECT statements, each of which returns < 67,000 rows, and then do everything else the same.
Split your recordset. Perform one SELECT that extracts all 90,000 rows, and split it on Excel import step.
private List<DataTable> CloneTable(DataTable tableToClone, int countLimit)//Split function { List<DataTable> tables = new List<DataTable>(); int count = 0; DataTable copyTable = null; foreach (DataRow dr in tableToClone.Rows) { if ((count++ % countLimit) == 0) { copyTable = new DataTable(); copyTable = tableToClone.Clone(); copyTable.TableName = "Sample" + count; tables.Add(copyTable); } copyTable.ImportRow(dr); } return tables; } protected void LinkReport_Click(object sender, EventArgs e) { DataTable dt2 = (DataTable)ViewState["dtab"]; List<DataTable> dt1 = CloneTable(dt2, 5); DataSet ds = new DataSet("dst"); for (int i = 0; i < dt1.Count; i++) { ds.Tables.Add(dt1[i]); } string filePath = Server.MapPath("Reports/").ToString() + "master.xls"; FileInfo file = new FileInfo(filePath); if (file.Exists) { file.Delete(); } Export(ds, filePath);// Export into Excel } Clone - The fastest method to create tables with original columns structure is Clone method. Export into Excel private void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception ex) { obj = null; } finally { GC.Collect(); } } public void Export(DataSet ds, string filePath) { string data = null; string columnName = null; int i = 0; int j = 0; Excel.Application xlApp; Excel.Workbook xlWorkBook; //Excel.Worksheet xlWorkSheet; Excel.Worksheet xlWorkSheet = null; object misValue = System.Reflection.Missing.Value; Excel.Range range; xlApp = new Excel.ApplicationClass(); xlWorkBook = xlApp.Workbooks.Add(misValue); //xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); for (int l = 0; l < ds.Tables.Count; l++) { xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(l + 1); xlWorkSheet.Cells[1, 1] = "Report"; xlWorkSheet.get_Range("A1:D1", Type.Missing).Merge(Type.Missing); xlWorkSheet.get_Range("A1", "D1").Font.Bold = true; xlWorkSheet.Cells.Font.Name = "Courier New"; if (l == 0) { xlWorkSheet.Name = "Sheet1"; } else if (l == 1) { xlWorkSheet.Name = "Sheet2"; } else if (l == 2) { xlWorkSheet.Name = "Sheet3"; } else if (l == 3) { xlWorkSheet.Name = "Sheet4"; } else if (l == 4) { xlWorkSheet.Name = "Sheet5"; } for (i = 0; i <= ds.Tables[l].Rows.Count - 1; i++) { for (j = 0; j <= ds.Tables[l].Columns.Count - 1; j++) { columnName = ds.Tables[l].Columns[j].ColumnName.ToString(); xlWorkSheet.Cells[3, j + 1] = columnName; data = ds.Tables[l].Rows[i].ItemArray[j].ToString(); xlWorkSheet.Cells[i + 5, j + 1] = data; } } } //for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) //{ // for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++) // { // data = ds.Tables[0].Rows[i].ItemArray[j].ToString(); // xlWorkSheet1.Cells[i + 1, j + 1] = data; // } //} xlWorkBook.SaveAs(filePath, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); // kill all excel processes Process[] pros = Process.GetProcesses(); for (int p = 0; p < pros.Length; p++) { if (pros[p].ProcessName.ToLower().Contains("excel")) { pros[p].Kill(); break; } } releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); } Try this One.. I have Worked out in Visual Studio 2005
DataTable[] splittedtables = dt.AsEnumerable() .Select((row, index) => new { row, index }) .GroupBy(x => x.index / Input From User) // integer division, the fractional part is truncated .Select(g => g.Select(x => x.row).CopyToDataTable()) .ToArray(); This should work.