Last active
July 20, 2020 15:03
-
-
Save sergiougalde/6247269 to your computer and use it in GitHub Desktop.
Imprimir con Java en una impresora de tickets Epson tmu 220En muchos lugares se habla de como imprimir con Java en una epson tmu 220 , he encontrado mucho sobre y he encontrado muchas recomendaciones de usar epson java pos (http://www.javapos.com/de
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
/** | |
* Class declaration | |
* | |
* @author | |
* @version 1.10, 08/04/00 | |
*/ | |
public class Impresora { | |
//Variables de acceso al dispositivo | |
private FileWriter fw; | |
private BufferedWriter bw; | |
private PrintWriter pw; | |
private String dispositivo = ""; // Cambiar Valor | |
private static String OS = System.getProperty("os.name").toLowerCase(); | |
/* Esta funcion inicia el dispositivo donde se va a imprimir */ | |
public void setDispositivo(String texto) { | |
dispositivo = texto; | |
if (dispositivo.trim().length() <= 0) { | |
if (OS.contains("win")) { | |
dispositivo = "LPT1";//Esto si es windows | |
} else { | |
dispositivo = "/dev/lp0";//Esto si es linux | |
} | |
} | |
try { | |
fw = new FileWriter(dispositivo); | |
bw = new BufferedWriter(fw); | |
pw = new PrintWriter(bw); | |
} catch (Exception e) { | |
System.out.print(e); | |
} | |
} | |
public void escribir(String texto) { | |
try { | |
pw.println(texto); | |
} catch (Exception e) { | |
System.out.print(e); | |
} | |
} | |
public void cortar() { | |
try { | |
char[] ESC_CUT_PAPER = new char[]{0x1B, 'm'}; | |
if (!this.dispositivo.trim().equals("pantalla.txt")) { | |
pw.write(ESC_CUT_PAPER); | |
} | |
} catch (Exception e) { | |
System.out.print(e); | |
} | |
} | |
public void avanza_pagina() { | |
try { | |
if (!this.dispositivo.trim().equals("pantalla.txt")) { | |
pw.write(0x0C); | |
} | |
} catch (Exception e) { | |
System.out.print(e); | |
} | |
} | |
public void setRojo() { | |
try { | |
char[] ESC_CUT_PAPER = new char[]{0x1B, 'r', 1}; | |
if (!this.dispositivo.trim().equals("pantalla.txt")) { | |
pw.write(ESC_CUT_PAPER); | |
} | |
} catch (Exception e) { | |
System.out.print(e); | |
} | |
} | |
public void setNegro() { | |
try { | |
char[] ESC_CUT_PAPER = new char[]{0x1B, 'r', 0}; | |
if (!this.dispositivo.trim().equals("pantalla.txt")) { | |
pw.write(ESC_CUT_PAPER); | |
} | |
} catch (Exception e) { | |
System.out.print(e); | |
} | |
} | |
public void setTipoCaracterLatino() { | |
try { | |
char[] ESC_CUT_PAPER = new char[]{0x1B, 'R', 18}; | |
if (!this.dispositivo.trim().equals("pantalla.txt")) { | |
pw.write(ESC_CUT_PAPER); | |
} | |
} catch (Exception e) { | |
System.out.print(e); | |
} | |
} | |
public void setFormato(int formato) { | |
try { | |
char[] ESC_CUT_PAPER = new char[]{0x1B, '!', (char) formato}; | |
if (!this.dispositivo.trim().equals("pantalla.txt")) { | |
pw.write(ESC_CUT_PAPER); | |
} | |
} catch (Exception e) { | |
System.out.print(e); | |
} | |
} | |
public void correr(int fin) { | |
try { | |
int i = 0; | |
for (i = 1; i <= fin; i++) { | |
this.salto(); | |
} | |
} catch (Exception e) { | |
System.out.print(e); | |
} | |
} | |
public void salto() { | |
try { | |
pw.println(""); | |
} catch (Exception e) { | |
System.out.print(e); | |
} | |
} | |
public void dividir() { | |
escribir("—————————————-"); | |
} | |
public void cerrarDispositivo() { | |
try { | |
pw.close(); | |
if (this.dispositivo.trim().equals("pantalla.txt")) { | |
java.io.File archivo = new java.io.File("pantalla.txt"); | |
java.awt.Desktop.getDesktop().open(archivo); | |
} | |
} catch (Exception e) { | |
System.out.print(e); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Main { | |
public static void main(String args[]) { | |
Impresora p = new Impresora(); | |
p.setDispositivo(""); | |
p.escribir((char) 27 + "m"); | |
p.setTipoCaracterLatino(); | |
p.setRojo(); | |
p.escribir("Esto es una prueba"); | |
p.setNegro(); | |
p.escribir("esto es negro" + (char) 130); | |
p.setFormato(24); | |
p.escribir("esto es negro con formato"); | |
p.setFormato(1); | |
p.escribir("esto es negro con formato"); | |
p.correr(10); | |
p.cortar(); | |
p.cerrarDispositivo(); | |
} | |
} |
Hola buen dia.
Sabes si este codigo sirve para comanderas epson?
Se cambia código porque no me había dado cuenta que al subirlo.. se había cambiado mucho el formato y me faltaban clases.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hola Sergio, estoy viendo tu codigo y tuve el problema con el "new Session()".
Acá en los comentarios pude ver algo un poco mas de eso para resolverlo. Gracias a esto estoy terminando un proyecto personal, en los cuales voy a mencionar tu codigo ya que me resulta de mucha ayuda.
Edit: falta una llave al final del codigo impresora.java.