I am trying to make an application to scan documents from a scanner and i have found an application based on the mmscomputing free library that i found in github https://github.com/ashishkataria/browserWebScanning
It shows a panel from where you can choose a scanner from a list of available ones and scan the document.
public void getScan()
{
try
{
scanner.acquire();
}
catch (ScannerIOException e1)
{
IJ.showMessage("Access denied! \nTwain dialog maybe already opened!");
e1.printStackTrace();
}
}
public Image getImage()
{
Image image = imp.getImage();
return image;
}
public void update(ScannerIOMetadata.Type type, ScannerIOMetadata metadata) {
if (type.equals(ScannerIOMetadata.ACQUIRED))
{
if(imp!=null)
{
jContentPane.remove(ipanel);
jContentPane.remove(cpanel);
jContentPane.remove(crpdpanel);
}
imp = new ImagePlus("Scan", metadata.getImage());
im = imp.getImage();
imagePanel = new ImagePanel(im);
imagePanel.updateUI();
imagePanel.repaint();
imagePanel.revalidate();
ClipMover mover = new ClipMover(imagePanel);
imagePanel.addMouseListener(mover);
imagePanel.addMouseMotionListener(mover);
ipanel = imagePanel.getPanel();
ipanel.setBorder(new LineBorder(Color.blue,1));
ipanel.setBorder(BorderFactory.createTitledBorder("Scanned Image"));
ipanel.setBounds(0, 30,600, 600);
ipanel.repaint();
ipanel.revalidate();
ipanel.updateUI();
jContentPane.add(ipanel);
jContentPane.getRootPane().revalidate();
jContentPane.updateUI();
cpanel = imagePanel.getUIPanel();
cpanel.setBounds(700, 30,300, 150);
cpanel.repaint();
cpanel.setBorder(new LineBorder(Color.blue,1));
cpanel.setBorder(BorderFactory.createTitledBorder("Cropping Image"));
cpanel.setBackground(Color.white);
jContentPane.add(cpanel);
jContentPane.repaint();
jContentPane.revalidate();
metadata.setImage(null);
try {
new uk.co.mmscomputing.concurrent.Semaphore(0, true).tryAcquire(2000, null);
} catch (InterruptedException e) {
IJ.error(e.getMessage());
}
}
else if (type.equals(ScannerIOMetadata.NEGOTIATE)) {
ScannerDevice device = metadata.getDevice();
try {
device.setResolution(100);
} catch (ScannerIOException e) {
IJ.error(e.getMessage());
}
try{
device.setShowUserInterface(true);
device.setResolution(100); }catch(Exception e){
e.printStackTrace(); }
}
else if (type.equals(ScannerIOMetadata.STATECHANGE)) {
System.out.println("Scanner State "+metadata.getStateStr());
System.out.println("Scanner State "+metadata.getState());
if ((metadata.getLastState() == 3) && (metadata.getState() == 4)){}
} else if (type.equals(ScannerIOMetadata.EXCEPTION)) {
IJ.error(metadata.getException().toString());
}
}
I am trying to make it handle multiple documents and save in a pdf file, this library handles only 1 document right now.
i want to know how can i save the images in a buffer or something else and rescan until the user is done .
and is there a function that can save those images in a pdf file ?
Related
I use the following code to record and play sounds with Java, but the volume is too low, how to make it louder, at least 2,3 times louder ?
public void Record_Audio(String File_Path,AudioFileFormat.Type File_Type)
{
try
{
audioFormat=getAudioFormat(); // Get things set up for capture
DataLine.Info dataLineInfo=new DataLine.Info(TargetDataLine.class,audioFormat);
targetDataLine=(TargetDataLine)AudioSystem.getLine(dataLineInfo);
//Create a thread to capture the microphone data into an audio file and start the thread running. It will run
// until the Stop button is clicked. This method will return after starting the thread.
new Record_Thread(File_Path,File_Type).start();
}
catch (Exception e)
{
System.out.println(e);
System.exit(0);
}
}
private void Play_Audio_Recording()
{
File Audio_File=new File(Current_Folder_Path+File_Name_ComboBox.getSelectedItem().toString().trim()+"."+Get_Audio_File_Type());
try
{
Audio_Clip=Applet.newAudioClip(Audio_File.toURI().toURL());
Audio_Clip.play();
}
catch (Exception e) { e.printStackTrace(); }
}
class Record_Thread extends Thread
{
String File_Path;
AudioFileFormat.Type File_Type;
Record_Thread(String File_Path) { this(File_Path,AudioFileFormat.Type.WAVE); }
Record_Thread(String File_Path,AudioFileFormat.Type File_Type)
{
this.File_Path=File_Path;
this.File_Type=File_Type;
}
public void run()
{
Audio_File=new File(File_Path);
try
{
targetDataLine.open(audioFormat);
targetDataLine.start();
AudioSystem.write(new AudioInputStream(targetDataLine),File_Type,Audio_File);
}
catch (Exception e) { e.printStackTrace(); }
}
}
You would use the FloatControl.Type (https://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/FloatControl.Type.html) to set either the volume or the master gain. Something like:
targetDataLine=(TargetDataLine)AudioSystem.getLine(dataLineInfo);
javax.sound.sampled.FloatControl c = (FloatControl)targetDataLine.getControl(FloatControl.Type.VOLUME);
c.setValue(c.getMaximum());
might work.
I am trying to create an app in java using OpenCV to grab videostream from web service which is a camera system with couple of cameras and a recording device.
I have found the address "rtsp://login:pass#IP address:Port/cam/realmonitor?channel=1&subtype=0" for accessing the camera on channel 1.
For opening camera stream I have used this code (curently it catches a local usb camera):
VideoCapture cap;
Mat2Image mat2Img = new Mat2Image();
public VideoGrabber(){
cap = new VideoCapture(0);
try {
System.out.println("Sleeping..");
Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Camera on..");
cap.open("0");
if(!cap.isOpened()){
System.out.println("Camera Error");
}
else{
System.out.println("Camera OK?");
}
}
After grabbing the video stream I put it into a JFrame.
I think I should put the video streaming service address in cap.open( ... ) but using rtsp://login:pass#http://192.168.1.14:8006/cam/realmonitor?channel=1&subtype=0 gave me "Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Width (0) and height (0) must be > 0".
Please help,
EDIT
I have found out that rtsp://login:pass#http://192.168.1.14:554/cam/realmonitor?channel=1&subtype=0 works in vlc but still no luck in opencv.
EDIT #2
Ok. After playing with vlcl, gstreamer and most of the popular solutions it just started working. I don't know if it wasn't bad rtsp address after all. Code:
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
//load the library of opencv
}
VideoCapture cap;
Mat2Image mat2Img = new Mat2Image();
Mat matFilter = new Mat();
public VideoGrabber(){
cap = new VideoCapture();
try {
System.out.println("Sleeping..");
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Camera on..");
cap.open("rtsp://login:pass#192.168.1.14:554/cam/realmonitor?channel=1&subtype=0");
if(!cap.isOpened()){
System.out.println("Camera Error");
}
else{
System.out.println("Camera OK?");
}
}
Answering my question and for Fouad I post the working code:
I am guessing the answer was loading ffmpeg dll.
//all the imports
public class App {
static {
String path = null;
try {
//I have copied dlls from opencv folder to my project folder
path = "E:\\JAVA Projects\\OpenCv\\RTSP Example\\libraries";
System.load(path+"\\opencv_java310.dll");
System.load(path+"\\opencv_ffmpeg310_64.dll");
} catch (UnsatisfiedLinkError e) {
System.out.println("Error loading libs");
}
}
public static void main(String[] args) {
App app = new App();
//Address can be different. Check your cameras manual. :554 a standard RTSP port for cameras but it can be different
String addressString = "rtsp://login:password#192.168.1.14:554/cam/realmonitor?channel=11&subtype=0";
Mat mat = new Mat();
VideoCapture capturedVideo = new VideoCapture();
boolean isOpened = capturedVideo.open(addressString);
app.openRTSP(isOpened, capturedVideo, mat);
}
public void openRTSP(boolean isOpened, VideoCapture capturedVideo, Mat cameraMat) {
if (isOpened) {
boolean tempBool = capturedVideo.read(cameraMat);
System.out.println("VideoCapture returned mat? "+tempBool);
if (!cameraMat.empty()) {
System.out.println("Print image size: "+cameraMat.size());
//processing image captured in cameraMat object
} else {
System.out.println("Mat is empty.");
}
} else {
System.out.println("Camera connection problem. Check addressString");
}
}
}
I've succeeded onto transferring files onto my Dropbox account.
But when i share my app for testing with other members of the project, they can't transfer the files on their own Dropbox account.
And when they test the functionality, it transfers automatically onto my account.
Is there something I'm missing out here?
I know that i don't need to add manually users, they can automatically transfer the files they want.
Here's a snip of the method i used :
private void UploadToDropboxFromPath(String uploadPathFrom, String uploadPathTo) {
Toast.makeText(getApplicationContext(), "Upload file ...", Toast.LENGTH_SHORT).show();
final String uploadPathF = uploadPathFrom;
final String uploadPathT = uploadPathTo;
Thread th = new Thread(new Runnable() {
public void run() {
File tmpFile = null;
try {
tmpFile = new File(uploadPathF);
} catch (Exception e) {
e.printStackTrace();
}
FileInputStream fis = null;
try {
fis = new FileInputStream(tmpFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
dropboxAPI.putFileOverwrite(uploadPathT, fis, tmpFile.length(), null);
} catch (Exception e) {
}
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "File successfully uploaded.", Toast.LENGTH_SHORT).show();
}
});
}
});
th.start();
}
public class Midlet extends MIDlet implements CommandListener{
Player p;
public void startApp() {
Display.getDisplay(this).setCurrent(new SongsList(this));
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
public void commandAction(Command cmnd, Displayable dsplbl) {
if (cmnd.getLabel().equals("Exit"))
{
destroyApp(true);
}
else
{
try {
//InputStream is = getClass().getResourceAsStream("/res/getlucky.mpeg");
//p = Manager.createPlayer(is, "audio/mpeg");
p = Manager.createPlayer("http://puu.sh/6n9jC.mp3");
p.realize();
p.start();
} catch (IOException ex) {
ex.printStackTrace();
} catch (MediaException ex) {
ex.printStackTrace();
}
}
}
}
this is the songslist class :
public class SongsList extends List{
public SongsList(Midlet midlet)
{
super("Songs", List.IMPLICIT);
append("get lucky", null);
addCommand(new Command("Exit", Command.EXIT, 0));
addCommand(new Command("Select", Command.OK, 0));
setCommandListener(midlet);
}
}
tried use via file stored in project (its under src/res):
inputStream = getClass().getResourceAsStream("res/getlucky.mpg");
audioPlayer = Manager.createPlayer(inputStream, "audio/mpg");
as well as from HTTP:
//audioPlayer = Manager.createPlayer("http://puu.sh/6n9jC.mp3");
Nothing works, what am I doing wrong?
EDIT:
I've tried to delete my application and just copy paste it to a new project and it worked for some reason.. now I encounter new problems:
1) I try to play a song - this is the link http://puu.sh/6n9jC.mp3
its not playing so I guess there's a limited file size for what can be played can someone tell me what is this limit ?
2) Im trying to record the audio with RecordPlayer but its always null
public AudioAnalyzer()
{
try {
thread = new Thread(this);
recordFinished = false;
//inputStream = getClass().getResourceAsStream("res/getlucky.mpg");
//audioPlayer = Manager.createPlayer(inputStream, "audio/mpg");
audioPlayer = Manager.createPlayer("http://puu.sh/35YTG.mp3");
//audioPlayer = Manager.createPlayer("http://puu.sh/6n9jC.mp3");
audioPlayer.realize();
System.out.println(System.getProperty("supports.audio.capture"));
recordControl = (RecordControl)audioPlayer.getControl("RecordControl");
recordOutput = new ByteArrayOutputStream();
recordControl.setRecordStream(recordOutput);
recordControl.startRecord();
audioPlayer.start();
//thread.start();
} catch (MediaException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
I even tried to print if the system is supporting audio capture and the result were true but I get NullPointException at this line :
recordOutput = new ByteArrayOutputStream();
although I tried to get the recordcontrol from the player it is still null :
recordControl = (RecordControl)audioPlayer.getControl("RecordControl");
I think I read that it'll always give NullPointerException unless you run it on a real device and not an emulator is that true ? can someone verify it ? and if so what can I do if I don't own a device currently any other way to use recordcontrol feature in emulator (assuming recordcontrol isn't working on emulators).
File size is 8MB (maybe play on your phone), try to this code
public void initMedia(final String aFileUrl) {
if (m_player == null) {
try {
m_player = Manager.createPlayer(aFileUrl);
m_player.addPlayerListener(this);
m_player.realize();
m_player.prefetch();
m_volumeControl = (VolumeControl) m_player.getControl("VolumeControl");
} catch (IOException ex) {
} catch (Exception ex) {
} catch (OutOfMemoryError e) {
}
}
}
In your code, i guess you miss "m_player.prefetch()", try this. And print your Exception message...
This code in general for file, resourcce, http...
public void initMedia(final String aProtocol, final String aMediaSource) {
if (m_player == null) {
try {
if (aMediaSource.indexOf("file://") == 0) {
InputStream iRecordStream = Connector.openInputStream(aMediaSource);
m_player = Manager.createPlayer(iRecordStream, "audio/amr");
} else {
m_player = Manager.createPlayer(aProtocol);
}
m_player.addPlayerListener(this);
m_player.realize();
boolean isPrefetch = true;
try {
m_player.prefetch();
} catch (Exception ex) {
isPrefetch = false;
}
// trick to pass prefetch error
if (!isPrefetch) {
if (m_player != null) {
m_player.close();
m_player = null;
}
if (aMediaSource.indexOf("file://") == 0) {
InputStream iRecordStream = Connector.openInputStream(aMediaSource);
m_player = Manager.createPlayer(iRecordStream, "audio/amr");
} else {
m_player = Manager.createPlayer(aProtocol);
}
m_player.addPlayerListener(this);
m_player.realize();
m_player.prefetch();
}
m_volumeControl = (VolumeControl) m_player.getControl("VolumeControl");
} catch (IOException ex) {
} catch (Exception ex) {
} catch (OutOfMemoryError e) {
}
}
}
In general when it comes to J2ME development, you should always test your app on multiple real devices.
Emulators can't be trusted.
Also, J2ME is very fragmented, and various devices have various bugs and behaves differently with the same code. This will affect any app on many areas. One area being audio playback.
For example, some devices requires that you use the realize() and prefetch() methods, while other devices will crash if you use prefetch(). The only possible solution (if you wish to support as many devices as possible) is to use multiple try/catch blocks.
See this link for a detailed explanation and other tips'n'tricks on audio playback with MIDP2.0
http://indiegamemusic.com/help.php?id=1
Im trying to use ZXing library to develop a Java project for decoding a QR code. However, some of the image containing QR code can not be decoded by running my project, but these are working fine with Online ZXing decoder. I am just curious does the ZXing released version is the same as they are using for Online decoder? or they have tweaked the online version. I'm pulling my hair because of this confusion.
public class Validator implements IValidator {
private static Logger logger = Logger.getLogger(Validator.class);
private BufferedImage currentImage;
private String resultText;
private float moduleSize;
private ResultPoint[] patternCenters;
private int blockSizePower;
public Validator(BufferedImage imageFile) {
this.currentImage = imageFile;
setLuminanceThreshold(3); //default value used by validator
}
public Validator(File imageFile) {
// take input image file and store in a BufferedImage variable
try {
currentImage = ImageIO.read(imageFile);
} catch (IOException e) {
logger.error("Image cannot be opened. There is no such image file. ", e);
}
}
/**
* <p>Validating the QR code</p>
*
* #return true if the QR code can be decoded
*/
#Override
public boolean validateQRCode() {
return validateQRCode(null);
}
public boolean validateQRCode(Hashtable outValues) {
return validateQRCode(outValues, true);
}
// if localLuminanceCheck == true then call HybridBinarizer, otherwise call GlobalHistogramBinarizer
public boolean validateQRCode(Hashtable outValues, boolean localLuminanceCheck)
{
return validateQRCode(outValues, true, false);
}
public boolean validateQRCode(Hashtable outValues, boolean localLuminanceCheck, boolean scale) {
if (scale)
{
try {
this.currentImage = Thumbnails.of(currentImage).size(275, 275).asBufferedImage();
} catch (IOException e) {
logger.error("Image cannot be scaled. ", e);
}
}
// finding luminance of the image
LuminanceSource lumSource = new BufferedImageLuminanceSource(currentImage);
Binarizer qrHB;
if (!localLuminanceCheck) {
qrHB = new GlobalHistogramBinarizer(lumSource);
} else {
// creating binary bitmap from Black-White image
qrHB = new HybridBinarizer(lumSource);
((HybridBinarizer) qrHB).setBLOCK_SIZE_POWER(blockSizePower);
}
BinaryBitmap bitmap = new BinaryBitmap(qrHB);
try {
currentImage = MatrixToImageWriter.toBufferedImage(bitmap.getBlackMatrix());
} catch (NotFoundException e) {
logger.error("cannot find any bit matrix.", e);
}
Hashtable<DecodeHintType, Object> hint = new Hashtable<DecodeHintType, Object>();
hint.put(DecodeHintType.TRY_HARDER, BarcodeFormat.QR_CODE);
QRCodeReader QRreader = new QRCodeReader();
try {
// decodes the QR code
Result result = QRreader.decode(bitmap, hint);
resultText = result.getText();
return true;
} catch (NotFoundException e) {
logger.info("cannot detect any QR code (no enough finder patterns).");
return false;
} catch (ChecksumException e) {
logger.info("cannot recover the QR code. Too much data errors.");
return false;
} catch (FormatException e) {
logger.info("QR code cannot be decoded.");
return false;
} catch (FinderPatternNotFoundException e) {
// if no Finder Pattern has been found, it may be the color of
// QR is inverted. So we invert the QR and try one more time
Binarizer invertHB;
if (!localLuminanceCheck) {
invertHB = new GlobalHistogramBinarizer(lumSource);
} else {
invertHB = new HybridBinarizer(lumSource);
((HybridBinarizer) invertHB).setBLOCK_SIZE_POWER(blockSizePower);
}
// get the inverted Black-White matrix
BitMatrix invertBlackMatrix = null;
try {
invertBlackMatrix = invertHB.getBlackMatrix();
} catch (NotFoundException e1) {
logger.error(e1);
}
int invertWidth = currentImage.getWidth();
int invertHeight = currentImage.getHeight();
// flip each bit in the inverted BitMatrix
for (int x = 0; x < invertWidth; x++) {
for (int y = 0; y < invertHeight; y++) {
invertBlackMatrix.flip(x, y);
}
}
currentImage = MatrixToImageWriter.toBufferedImage(invertBlackMatrix);
// get luminance source from inverted image
lumSource = new BufferedImageLuminanceSource(currentImage);
Binarizer afterInvertHB;
if (!localLuminanceCheck) {
afterInvertHB = new GlobalHistogramBinarizer(lumSource);
} else {
// creating binary bitmap from Black-White image
afterInvertHB = new HybridBinarizer(lumSource);
((HybridBinarizer) afterInvertHB).setBLOCK_SIZE_POWER(blockSizePower);
}
BinaryBitmap invertBitMap = new BinaryBitmap(afterInvertHB);
// decoding inverted QR
QRCodeReader invertQRreader = new QRCodeReader();
try {
Result invertResult = invertQRreader.decode(invertBitMap, hint);
resultText = invertResult.getText();
System.out.println("Out put data is: " + resultText);
return true;
} catch (NotFoundException e1) {
logger.info("cannot detect any QR code (no enough finder patterns).");
return false;
} catch (ChecksumException e1) {
logger.info("cannot recover the QR code. Too much data errors.");
return false;
} catch (FormatException e1) {
logger.info("QR code cannot be decoded.");
return false;
} catch (FinderPatternNotFoundException e1) {
logger.info("Cannot confirm where all three Finder Patterns are! ");
return false;
} catch (Exception e1) {
logger.error(e1);
return false;
}
} catch (Exception e) {
logger.error(e);
return false;
}
}
}
It's not different, it's probably that you are not using TRY_HARDER mode, or are not trying both binarizers. The online version will do those things.