Compilation error on Zxing - java

Since I don't own a G1 for development purposes, I am doing my best with the emulator.
This said, I am trying to scan a JPEG image or a PNG image in my sdCard, with the ZXing (Zebra Zrossing) library.
I tried to change the code in the Android project, so it would scan from an image in the sdCard, instead from the camera, without any luck.
What I did next is probably the root of my problem.
I tried to use the JAVASE code, within a new Android Project, to provide an image to the "modified" CommandLineRunner, and here is the thing:
Eclipse would build the project, but won't run it.
The VM log throuws me this message among others:
**02-08 20:47:45.916: WARN/dalvikvm(619): VFY: unable to resolve static method 939: Ljavax/imageio/ImageIO;.read (Ljava/io/File;)Ljava/awt/image/BufferedImage;
02-08 20:47:45.926: WARN/dalvikvm(619): VFY: rejecting opcode 0x71 at 0x0004
02-08 20:47:45.926: WARN/dalvikvm(619): VFY: rejected Lcom/magoco/fread/FRead;.decode2 (Ljava/io/File;Ljava/util/Hashtable;Ljava/lang/String;)Ljava/lang/String;
02-08 20:47:45.926: WARN/dalvikvm(619): Verifier rejected class Lcom/magoco/fread/FRead;
02-08 20:47:45.926: WARN/dalvikvm(619): Class init failed in newInstance call (Lcom/magoco/fread/FRead;)
02-08 20:47:45.926: DEBUG/AndroidRuntime(619): Shutting down VM
02-08 20:47:45.926: WARN/dalvikvm(619): threadid=3: thread exiting with uncaught exception (group=0x40010e28)
02-08 20:47:45.937: ERROR/AndroidRuntime(619): Uncaught handler: thread main exiting due to uncaught exception
02-08 20:47:45.946: ERROR/AndroidRuntime(619): java.lang.VerifyError: com.magoco.fread.FRead
**
I searched the web for an answer and I got someone saying that this is common error on the Dalvikvm due to the fact that there is might be a class or library that was precompiled (true, outside Eclipse) and the VM wouldn't be able to use it.
I am posting my code in the main Activity:
package com.magoco.fread;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.TextView;
import com.google.zxing.DecodeHintType;
import com.google.zxing.MonochromeBitmapSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.client.result.ParsedResult;
import com.google.zxing.client.result.ResultParser;
public class FRead extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) this.findViewById(R.id.BarcodeResult);
try {
tv.setText(this.decodeOneArgument2("", false));
} catch (Exception e) {
e.printStackTrace();
}
}
public String decodeOneArgument2(String argument, boolean dumpResults)
throws Exception {
String barcode = "";
// File inputFile = new File(argument);
File inputFile = new File("sdcard/dcim/pueblo.JPG");
/* TESTING THAT I'VE GOT A FILE */
System.out.println("FILE " + inputFile.toString());
// decode(new URI(argument), hints);
decode2(inputFile, null, barcode);
return barcode;
}
public String decode2(File f, Hashtable<DecodeHintType, Object> hints,
String barcode) throws IOException {
/* IF I COMMENT THE NEXT LINE, IT RUNS BUT OF COURSE NO RESULT */
BufferedImage image;
try {
image = ImageIO.read(f);
} catch (IllegalArgumentException iae) {
throw new FileNotFoundException("Resource not found: " + f);
}
try {
MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(
image);
Result result = new MultiFormatReader().decode(source, hints);
ParsedResult parsedResult = ResultParser.parseResult(result);
barcode = " format: " + result.getBarcodeFormat()+ result.getText() + "\nParsed result:\n"+ parsedResult.getDisplayResult();
System.out.println(" format: " + result.getBarcodeFormat()+ result.getText() + "\nParsed result:\n"+ parsedResult.getDisplayResult());
//return result;
} catch (ReaderException e) {
System.out.println(": No barcode found");
return null;
}
return barcode;
}
}
Thanks in Advance
monn3t

Hai,
I tried to extract the data from the Barcodes and these are the steps i followed.
1.Download ZXing 1.3 and extract it.
2.Add the core/src and androidtest/src from the extracted zxing floder to the android application by setting the property.
use this link to set the property,http://groups.google.com/group/zxing/browse_thread/thread/7d8693e6e42408f2
Now include the following code,
package payoda.android.zxingApp;
import android.app.Activity;
import android.database.CursorJoiner.Result;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.TextView;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MonochromeBitmapSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Reader;
import com.google.zxing.ReaderException;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.client.androidtest.*;
import com.google.zxing.common.BaseMonochromeBitmapSource;
public class ZXingApplication1 extends Activity
{
TextView tv;
com.google.zxing.Result result;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.text);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.bar2);
MultiFormatReader reader1=new MultiFormatReader();//For all barcode formats
QRCodeReader reader=new QRCodeReader();//Only for QRCode format
try
{
result=reader1.decode(new RGBMonochromeBitmapSource(bitmap));
tv.setText(result.getText());
}
catch (Exception e)
{
tv.setText("Within Catch block");
}
}
}
This may be helps you.

This is Sean from the project in question.
You are trying to use the code intended for Java SE in Android. Some of the libraries in Java SE are not in Android, like ImageIO.
Look at the code in android/ which uses Android-specific classes to load images.

Related

python flask-socketio TypeError: a bytes-like object is required, not 'str'

I started to use Socketio, and I got a problem. I can't send a simple message to my flask session. My Java code:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.URISyntaxException;
import io.socket.client.IO;
import io.socket.client.Socket;
public class MainActivity extends AppCompatActivity {
Socket socket;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
socket = IO.socket("http://192.168.8.101:8080/send");
} catch (URISyntaxException e) {
}
}
public void TestButton(View v){
Log.d("send","before send");
JSONObject obj = new JSONObject();
try {
obj.put("message", "hi");
obj.put("binary", new byte[42]);
socket.emit("mes", obj);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
My Python code:
from flask import Flask
from flask_socketio import SocketIO
app = Flask(__name__)
socketio = SocketIO(app)
#socketio.on("mes", namespace="/send")
def chat_message(message):
print("message:")
if __name__ == "__main__":
print("start")
socketio.run(app, host="0.0.0.0", port=8080)
The error:
TypeError: a bytes-like object is required, not 'str' <Greenlet at 0x24118773178: _handle_and_close_when_done(<bound method WSGIServer.handle of <WSGIServer at , <bound method StreamServer.do_close of <WSGIServer, (<gevent._socket3.socket [closed] object, fd=-1, )> failed with TypeError
I hope someone is able to help me with this problem. I've searched everywhere and i can't find my answer.
I think you use python3 with the gevent and gevent-websocket plugin.
gevent-websocket does not support python3 yet.
You have to options here:
Uninstall gevent and gevent-websocket and use eventlet.
Fix the code geventwebsocket/handler.py at line 236.
if b'101' not in self.status:
I hope I could help you.

getting error in CODEC_ID_MPEG1VIDEO and PIX_FMT_YUV420P

I am trying to create a code which will convert images into video. I have surfed a lot and find some method to do it using javacv. now i am getting variable not found error in these lines.
recorder.setCodecID( CODEC_ID_MPEG1VIDEO);
recorder.setPixelFormat( PIX_FMT_YUV420P);
I have imported all the jar files into my library as well but still getting this error. Some quick help would be appreciated. I have gone through the below link for the importing the javacv into my workspace.
How to Import JavaCV libraries to Android Project
package com.example.photoaday;
import static com.googlecode.javacv.cpp.opencv_core.cvReleaseImage;
import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
import static com.googlecode.javacv.cpp.opencv_highgui.cvShowImage;
import static com.googlecode.javacv.cpp.opencv_highgui.cvWaitKey;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_GAUSSIAN;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvSmooth;
import com.googlecode.javacv.FFmpegFrameRecorder;
import com.googlecode.javacv.cpp.opencv_core;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Recordvideo extends Activity {
// #Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create);
opencv_core.IplImage img = cvLoadImage("/sdcard/folder/img1.jpg");
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("/sdcard/folder/test.mpeg",200,150);
try {
recorder.setCodecID( CODEC_ID_MPEG1VIDEO);
recorder.setFrameRate(30);
recorder.setPixelFormat( PIX_FMT_YUV420P);
recorder.start();
for (int i=0;i<100;i++)
{
recorder.record(img);
}
recorder.stop();
}
catch (Exception e){
e.printStackTrace();
}
}
}
I assume that your compiler gives error as follows
error: ‘CODEC_ID_MPEG1VIDEO’ was not declared in this scope
error: ‘PIX_FMT_YUV420P’ was not declared in this scope
then solution for error is
change : ‘CODEC_ID_MPEG1VIDEO’ to 'AV_CODEC_ID_MPEG1VIDEO'
change : ‘PIX_FMT_YUV420P’ to 'AV_PIX_FMT_YUV420P'

Beginning Android 4 Games Development error

In the book Beginning Android 4 games development by Mario Zechner and Robert Green I am following along to start making a game of my own. We create a framework for the game first and then implement it into one class, and I got one error which I could not figure out. The error occured when I tried to instantiate something from a class called AndroidFileIO.
This is how the book describes how to instantiate it:
FileIO fileIO
fileIO = new AndroidFileIO(getAssets());
and the class it gets it from is:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.os.Environment;
import android.preference.PreferenceManager;
import com.example.android4gamedevtut.FileIO;
public class AndroidFileIO implements FileIO{
Context context;
AssetManager assets;
String externalStoragePath;
public AndroidFileIO(Context assetManager) {
this.context = assetManager;
this.assets = assetManager.getAssets();
this.externalStoragePath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + File.separator;
}
#Override
public InputStream readAsset(String fileName) throws IOException {
return assets.open(fileName);
}
#Override
public InputStream readFile(String fileName) throws IOException {
return new FileInputStream(externalStoragePath + fileName);
}
#Override
public OutputStream writeFile(String fileName) throws IOException {
return new FileOutputStream(externalStoragePath + fileName);
}
public SharedPreferences getPreferences() {
return PreferenceManager.getDefaultSharedPreferences(context);
}
}
The error that is stopping me is saying "The constructor AndroidFileIO(AssetManager) is undefined" and eclipses suggests too fixes to the problem. The first being to "change the constructor AndroidFileIO(Context) to AndroidFileIO(AssetManager)" and the second being "create a constructor AndroidFileIO(AssetManager)". Please answer this question in simple terms I am very new to java.
you should use
fileIO = new AndroidFileIO(this);
instead of getAsset();
refer to line 54 in https://code.google.com/p/beginning-android-games-2/source/browse/trunk/ch09-jumper/src/com/badlogic/androidgames/framework/impl/GLGame.java?r=4

Cordova cannot find symbol error when build android project with self-built plugin (call for native java code)

I tried to run an android project in Cordova with self-build plugin,
Heres the code in JS and java for the plugin
var cordova = require('cordova');
var Carrier = function() {};
Carrier.prototype.getCarrierCode = function(success, error) {
cordova.exec(success, error, 'CarrierPlugin', 'getCarrierCode', []);
};
var carrier = new Carrier();
module.exports = carrier;
this is the java code:
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Date;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.content.ContentResolver;
public class CarrierPlugin extends CordovaPlugin{
public static final String TAG = "CarrierPlugin";
public static final String ACTION_GET_CARRIER_CODE = "getCarrierCode";
//public TelephonyManager tm;
#Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
// TODO Auto-generated method stub
super.initialize(cordova, webView);
}
#Override
public boolean execute(String action, CordovaArgs args,
CallbackContext callbackContext) throws JSONException{
callbackContext.success("run it");
return true;
}
}
the error I got is "cannot find symbol :"
the strange thing is that, even if I change the code in the CarrierPlugin.java(delete the JSONException in line 16), it reported the same error.
My Eclipse says, you should import CordovaInterface, CordovaWebView and CordovaArgs.
And you might be missing package declaration as well, depending wherever you want to put your plugin's java files, such as:
package com.yourdomain.etc in the very first line of your Java file.

how to write image data to a text file as a base 64 string?

im trying to write image data to a text file as a base 64 string its working if it doesnt contain to much information but when it contains quite a bit of information it crashes the application saying that its out off memory below is my code:
package com.search.visual;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;
import android.widget.Toast;
public class SavePhotoTask extends AsyncTask<byte[], String, String>{
String name;
byte[] num;
#Override
protected String doInBackground(byte[]... params) {
// TODO Auto-generated method stub
File photo = new File(Environment.getExternalStorageDirectory(),".vis/image.xml");
name = Base64.encodeToString(params[0], 0);
//return base64String;
if (photo.exists()){
photo.delete();
}
try{
FileWriter fi = new FileWriter(photo.getPath());
fi.write("<Image>\n\t<Data>");
fi.write(name);
fi.write("</Data>\n</Image>");
fi.close();
}catch (IOException e) {
// TODO: handle exception
Log.e("pictureDemo","exception", e);
}
return (null);
}
}
am i doing something wrong or is there a better way to do this?
thank you in advance
Sounds like you're simply running out of RAM. One reason this is happening is because you're holding the entire original image and the entire base-64 encoded image in memory, all at once, even if only briefly. Try writing the data out a chunk at a time in a for loop, using the variation:
encodeToString(byte[] input, int offset, int len, int flags)
And encoding and writing a few thousand bytes at a time

Categories

Resources