package
com.prodPrint
import
java.io.File
import
java.util.HashMap
import
org.eclipse.swt.SWT
import
org.eclipse.swt.ole.win32.OleAutomation
import
org.eclipse.swt.ole.win32.OleControlSite
import
org.eclipse.swt.ole.win32.OleFrame
import
org.eclipse.swt.ole.win32.Variant
import
org.eclipse.swt.widgets.Composite
import
org.eclipse.swt.widgets.Display
import
org.eclipse.swt.widgets.Shell
import
com.lppx2.OleDispatch
public
class
ActiveXPrinter
extends
Composite{
private
OleFrame
myFrame
=
new
OleFrame(this,
SWT.NONE)
//
Microsoft
Internet
Explorer
ProgID:
Shell.Explorer.2
//
Codesoft
ProgID
:
Lppx2.Application
private
String
progId
=
"Lppx2.Application"
private
OleControlSite
controlSite
private
OleAutomation
automation
private
OleDispatch
appActiveDoc
private
OleDispatch
appDocs
File
currentFile
public
ActiveXPrinter(Composite
parent,
int
style)
{
super(parent,
style)
myFrame
=
new
OleFrame(this,
SWT.NONE)
controlSite
=
new
OleControlSite(myFrame,
SWT.NONE,
progId)
automation
=
new
OleAutomation(controlSite)
}
public
void
(String
fileName)
{
currentFile
=
new
File(fileName)
initialize()
}
private
void
initialize()
{
try{
Variant
documents
=
(new
OleDispatch(automation)).Invoke("Documents",new
Variant[0])
appDocs
=
new
OleDispatch(documents.getAutomation())
if
(!currentFile.canRead()
)
{
System.out.println("Unable
to
read
file
:
"
+
currentFile)
}
else
{
Variant
file[]
=
new
Variant[1]
file[0]
=
new
Variant(currentFile.getAbsolutePath())
if
(appDocs.Invoke("Open",
file)
!=
null)
{
System.out.println(currentFile.getAbsolutePath()
+
"
is
opened
now")
}
else
{
System.out.println("Unable
to
open
"
+
currentFile.getAbsolutePath())
}
Variant
activeDocumentV
=
(new
OleDispatch(automation)).Invoke(
"ActiveDocument",
new
Variant[0])
try
{
appActiveDoc
=
new
OleDispatch(activeDocumentV.getAutomation())
}
catch
(Exception
excpt)
{
}
Variant[]
quantity
=
new
Variant[1]
quantity[0]
=
new
Variant(1)
if
(appActiveDoc.Invoke("PrintDocument",
quantity)
!=
null)
System.out.println("Print
OK")
else
{
System.out.println("Unable
to
!")
}
}
}catch(Exception
excpt)
{
System.out.println("You
can't
anything
until
a
document
is
opened
!")
excpt.printStackTrace()
return
}
}
public
static
void
main(String[]
args)
{
Display
display
=
Display.getDefault()
Shell
shell
=
new
Shell(display)
new
ActiveXPrinter(shell,
0).print("C:\Document1.Lab")
while
(!shell.isDisposed())
{
if
(!display.readAndDispatch())
{
display.sleep()
}
}
display.dispose()
}
}
一个比肢简毕较好的2d条形码咐渗解析条码的工具库从ZXING 中历芹剥离出来的
测试了一下,还可以,基本上清晰图片都可以识别
import java.awt.Frame
import java.awt.Graphics2D
import java.awt.Image
import java.awt.MediaTracker
import java.awt.image.BufferedImage
import com.google.zxing.MonochromeBitmapSource
import com.google.zxing.MultiFormatReader
import com.google.zxing.Reader
import com.google.zxing.ReaderException
import com.google.zxing.Result
import com.google.zxing.client.j2se.BufferedImageMonochromeBitmapSource
public class CodeReader {
/**
* @param args
* @throws ReaderException
* @throws InterruptedException
*/
public static void main(String[] args) throws ReaderException, InterruptedException {
Reader reader = new MultiFormatReader()
String imgPath =
"test.jpg"
Image image=java.awt.Toolkit.getDefaultToolkit().getImage(imgPath)
BufferedImage myImage = CodeReader.imageToBufImage(image)
MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(myImage)
Result result = reader.decode(source)
System.out.println(result.getText())
}
public static BufferedImage imageToBufImage(Image image) throws InterruptedException{
MediaTracker mt=new MediaTracker(new Frame())
mt.addImage(image,0)
mt.waitForID(0)
BufferedImage bufImage=new BufferedImage(image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_INT_BGR)
Graphics2D g2d=bufImage.createGraphics()
g2d.drawImage(image,0,0,null)
return bufImage
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)