*
The moose likes I/O and Streams and the fly likes printer not calling through this program.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "printer not calling through this program.." Watch "printer not calling through this program.." New topic
Author

printer not calling through this program..

sureshreddy govindu
Greenhorn

Joined: Jun 08, 2011
Posts: 29
import java.awt.Graphics;
import java.awt.PageAttributes;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.Attribute;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.MediaSizeName;

public class PrintImage {
static public void main(String args[]) throws Exception {



PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet() ;
pras.add(new Copies(1));

PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);
if (pss.length == 0)
throw new RuntimeException("No printer services available.");
PrintService ps = pss[0];
System.out.println("Printing to " + ps);
DocPrintJob job = ps.createPrintJob();
String s = "<STX>L<CR>"; // Print content of buffer, 1 label

// String s="oooooooo";




// Print content of buffer, 1 label

// String s="oooooooo";
byte[] by = s.getBytes();


Doc doc = new SimpleDoc(by, DocFlavor.BYTE_ARRAY.AUTOSENSE, null);

job.print(doc, pras);


/*String code="130000002000000BAR CODE N : UPC5<CR>";
BufferedImage img=new BufferedImage(100,100,BufferedImage.TYPE_INT_RGB);
Graphics g=img.getGraphics();
// g.setColor(Color.BLACK);
g.drawString(code,15,15);
ByteArrayOutputStream outstream=new ByteArrayOutputStream();
//ImageIO.write( img, "jpg", outstream);
byte[] buf=outstream.toByteArray();
String value=new String(buf);
System.out.println("Found printer: " + sPrinterName);
DocPrintJob job = psZebra.createPrintJob();
// Prepare string to send to the printer
String s = "<STX>L<CR>"; // Print content of buffer, 1 label

// String s="oooooooo";
byte[] by = s.getBytes();
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet() ;

MediaSizeName m = MediaSizeName.PERSONAL_ENVELOPE;
pras.add(m);
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
System.out.println(flavor.getMimeType());

//MIME type = "application/octet-stream";
//print data representation class name = "[B" (byte array).

System.out.println("Before callling ************");
Doc doc = new SimpleDoc(by, flavor, null);

System.out.println("Ready to print");

System.out.println(doc.getPrintData()+"---------");

job.print(doc, pras);

*/







}
}



we are using.. argox printer
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3582
    
  14

Hi Sureshreddy, please TellTheDetails and UseCodeTags. It will be easier for us to help you.
sureshreddy govindu
Greenhorn

Joined: Jun 08, 2011
Posts: 29
hi, i am using argox os-2140 barcode printer . i don't no how to pass barcode commands through java program. tell me how to access the printer . i am using above program is print the barcode. but it is not work. it's needed. below program is working cannon printer . but it's not working with argox os-2140 barcode printer .
thanks a lot for reply.



Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3582
    
  14

Well, I guess the barcode printer uses special drivers. If the manufacturers didn't provide a Java API, you will have to use native code to access the barcode printer driver.

There seems to be a download here for a command library. Maybe it comes with a readme or a manual. Take a look:
http://www.argox.com/content.php?sno=0000033&P_ID=82
sureshreddy govindu
Greenhorn

Joined: Jun 08, 2011
Posts: 29
thanks to reply,

how to use this commands through java program. please send me small example using printer commands . already i read this commands but how to use through java program.
we are using argox os-2140 printer(usb port) .
sureshreddy govindu
Greenhorn

Joined: Jun 08, 2011
Posts: 29
hi,

send me one small program for java how to send barcode string to barcode printer .
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3582
    
  14

You need Java Native Interface (JNI) to access the functions in the DLLs provided by the manufacturer. The readme will tell you what functions exists and how they work.

Google Java Native Interface or JNI to find a tutorial on how you can do this.
sureshreddy govindu
Greenhorn

Joined: Jun 08, 2011
Posts: 29
hi thanks to reply., How to use JNA library to communicate with barcode printer, if possible send me an example.

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;

/** Simple example of JNA interface mapping and usage. */
public class HelloWorld1 {

// This is the standard, stable way of mapping, which supports extensive
// customization and mapping of Java to native types.
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary)
Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"),
CLibrary.class);

void printf(String format, Object... args);
}

public static void main(String[] args) {
CLibrary.INSTANCE.printf("Hello, World\n");
for (int i=0;i < args.length;i++) {
CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);

}
}
}
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3582
    
  14

Have you read a tutorial on JNI/JNA? Have you figured out how to call functions in DLLs through JNI/JNA? Have you read the Readme that came with the command library?

Do all of these, then try to write some code for yourself. If you run into problems, post your code and explain where you are getting stuck and maybe someone can help you out. Don't forget to UseCodeTags or you will find it hard to receive help.
sureshreddy govindu
Greenhorn

Joined: Jun 08, 2011
Posts: 29
hi thanks to reply,



i am calling dll with java for using native api. below exception occur


Exception in thread "main" java.lang.UnsatisfiedLinkError: Form1.A_GetUSBBufferLen()I
at Form1.A_GetUSBBufferLen(Native Method)
at Form1.main(Form1.java:73)

using following code.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Your private static native int A_GetUSBBufferLen() method is not implemented in any of the DLLs you're loading.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
sureshreddy govindu
Greenhorn

Joined: Jun 08, 2011
Posts: 29
hi, thanks to reply


i have three dll file i.e related to those method.(which methods i used), i am accessing those method from java class(above). how to access that dll file through java file. give me one example how to access the method of dll with java (os)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Who created these DLLs? Does any of them contain the JNI implementation of the native methods in your class? Because you can't just load any DLL and call the functions from it - you need either JNI or JNA as a wrapper. The difference is that with JNI you write the code in C or C++ (mostly) that forms a bridge between the Java code and the DLLs, and with JNA you write similar code but in Java. JNA takes care of the translation from Java code to JNI code.
sureshreddy govindu
Greenhorn

Joined: Jun 08, 2011
Posts: 29
thanks to reply,

those dll's supplyed by argox os-2140 barcode printer company. these dll's used for implementing the program for barcode generation.
first --> register dll's with windows os(system 32 folder).
after i used these dll's through program.



Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3582
    
  14

Yes, like Rob said, you first have to create a wrapper around the DLLs supplied by the vendor. Java by itself does not know how to call functions in a DLL.

Create a JNI or JNA wrapper for the DLL you received. Then your Java code can either delegate to the JNI native code, or call the JNA methods. JNA might be easier, so find a JNA tutorial that explains how you can call methods from a DLL.
sureshreddy govindu
Greenhorn

Joined: Jun 08, 2011
Posts: 29
thanks to reply,

it was resolved, i got it with JNA . Thanks a lot ,

now i am facing another problem , deployed the application in the server , after deploying the application when access the application from client machine , the application see the printer on the server machine.., i want to access the application from client machine the application search the barcode printer from client machine only., (present it's seems server machine),

i used the code for accesing the usb printer is below

CLibrary.INSTANCE2.A_GetUSBBufferLen();

int nUSBDataLen;
byte[] pbuf;



nUSBDataLen=CLibrary.INSTANCE2.A_GetUSBBufferLen();
// nUSBDataLen = A_GetUSBBufferLen()+1;
pbuf = new byte[nUSBDataLen];
CLibrary.INSTANCE2.A_EnumUSB(pbuf);
CLibrary.INSTANCE2.A_CreateUSBPort(1);

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3582
    
  14

You can't do this. You need an application on the client's machine that uses the printer, and then sends the data to your server. It would be a huge security risk if server applications could run hardware on clients by themselves.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: printer not calling through this program..
 
Similar Threads
Printing Report in Struts2
Print simple jpeg image using java
Printing to Local Printer
Printing with 300dpi
argox os-2140 barcode printer how to pass barcode values to printers .