ZXing android use front camera - android

I'm trying to build a QR Code reader following this tutorial
http://code.tutsplus.com/tutorials/android-sdk-create-a-barcode-reader--mobile-17162
I managed to get everything working, except that I need the camera to be the front camera of my device instead of the rear camera. I can't find any place in the tutorial that allows me to change this. I tried following this answer, but I still could not get it to work.
Mainly, my issue is with importing the library. I get the following error.
operator is not allowed for source level below 1.7
When I set my compiler settings to 1.7, I get this
Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead
I'm not exactly very proficient with Android and I apologize if it might not be a good question.
So, any way for me to use ZXing with the front camera in my app? Any links?
Thank you very much.

The source code uses Java 7. Android does not require Java <= 6. You can see that the build provided in the project happily feeds Java 7 bytecode to dex and produces a working app. I am not sure what tool you are using that suggests otherwise. Maybe it is old.
You should not need to copy and compile the project's code though. Why is that necessary? use the core.jar file.
You don't need any of this to use the front camera. Just invoke by Intent (https://github.com/zxing/zxing/wiki/Scanning-Via-Intent) and set extra SCAN_CAMERA_ID to the ID of the camera you want -- usually 1 for the front one.
Example:
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
intent.putExtra("SCAN_CAMERA_ID", 1);

If you use IntentIntegrator, you can use setCameraId() to specify the front camera:
IntentIntegrator integrator = new IntentIntegrator(yourActivity);
integrator.setCameraId(1);
integrator.initiateScan();

After quite a few search i found how to use the front camera. There is this piece of code in com.google.zxing.client.android.camera.CameraConfigurationManager.java
public void openDriver(SurfaceHolder holder) throws IOException {
Camera theCamera = camera;
if (theCamera == null) {
theCamera = Camera.open();
if (theCamera == null) {
throw new IOException();
}
camera = theCamera;
}
theCamera.setPreviewDisplay(holder);
jus change the Camera.open() to Camera.open(1)
worked fine for me

Related

Android API Level 16 and higher Torch App

I found this Question but it doesn't work for me. (Nothing happens. Tested on Wiko Rainbow Jam)
Android - Camera2 : The easiest way to turn on the torch light
My App have to run on min. API Level 16! Is there an SupportCameraManager or an Library (under Apache), which can I use?
You Can Use this.
Initialize the NoobCameraManager singleton.
NoobCameraManager.getInstance().init(this);
You can optionally set the Log Level for debug logging. Logging uses LumberJack library. The default LogLevel is LogLevel.None
NoobCameraManager.getInstance().init(this, LogLevel.Verbose);
After that you just need to call the singleton to turn on or off the camera flash.
NoobCameraManager.getInstance().turnOnFlash();
NoobCameraManager.getInstance().turnOffFlash();
You can take care of the runtime permission to access Camera yourself or can allow the library to do it for you
NoobCameraManager.getInstance().takePermissions();
Note: The library will take permissions, if you haven't already, even without calling takePermissions() explicitly. This behavior may change in future.
It's easy to toggle Flash too
NoobCameraManager.getInstance().toggleFlash();
It's a good practice to release all the resources, once you're done.
NoobCameraManager.getInstance().release();
SOLUTION: The problem was that I only got the camera's parameters when turning on the light and turning off the light. This must apparently also be done in the constructor or in the overwriting onStart method.
#Override
protected void onStart() {
super.onStart();
camera = Camera.open(); //Also Call this
params = camera.getParameters(); //and this, in the Constructor
}
as fields:
private Camera camera;
Parameters params;
And then you can start the flashlight with these snippets of code: Android - Camera2 : The easiest way to turn on the torch light

How correctly manage the First ZXING Intent? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have implemented the ZXing functionnality in my App to read QR-Code.
Xzing runs with Barcode scanner via an exmplicit intent so the first you initiate the intent, it will ask to install barcode.
So it redirect you to the Play Store and the application gets installed.
Anyway, there is something not really easy to manage here as the Intent has been launched but we never get a return.
So the application waits for ever....
Is there any event to intercept or something like that...
Maybe there an OnActivity result for barcode installation already in the Integrator?
I tried to first deal with the PackageManager according to what is said here Zxing via intent
But in fact the test list.size() is successfull for ... Zalando Arghhh!!!
What happens?
The goal was to check if BarCode is installed, if no, ask for install it with a StartActivityForResult and once the install is complete, launch the Scan Intent.
Regards,
So,
It's a pity I don't have an answer to my question because I wonder in fact if it's no more common than only using Xzing.
The actual prefered Xzing implementation seems to be to use an intent to launch the barCode Scanner application.
The problem occurs if BarCode Scanner is not yet installed on your device.
In that case, the intent launch the play store to let you download it.
Here is the problem because, unless I didn't understand something and as I didn't get an answer, I think I'm not wrong, once BarCode Scanner is downloaded, installed and launched, you don't receive any answer to your sent Intent! Letting your user the ass between two chairs.
Another alternative could be to deal first with the package manager to ask it if Barcode Scanner is installed. That way, you discuss with the package manager to install the app and then launch your scan intent.
Unfortunately, I also tried that but for a reason I didn't understand asking to the package manager if "com.google.zxing.client.android.SCAN" is present gives a positive answer for ... Zalando !?! Arghhh!
Other possibility should be to integrate pieces of Xzing directly in your app.
I read many posts on this subject, all based on a different Xzing version and giving different methods and I didn't succed to get a working solution.
And obviously again, it seems that sean Owen clearly discourage this solution. What I really don't understand because in some situation, having to download a third party application while you are using your app could be vey uncomfortable and doesn't improve user experience. Well that's my point of view. I assume it even if it caused a serious downgrade in Stackoverflow !!
That's said my answer is to used Zbar. Zbar is no longer maintained on Android because its developper switched to Xzing!! well....
Anyway, it's an extremely efficient and eazy to use library and especially for the last reason, it's a high quality product.
Here I explain how to implement it:
-First download the archive https://sourceforge.net/projects/zbar/files/AndroidSDK/ZBarAndroidSDK-0.1.zip
-Then extract it of course.
You will get various directories:
*examples, with a CameraTest activity I encourage you to integrate in Eclipse to test it and also because your will get its components
*libs wher you will find the subdirs x86, armeab-v7a, armeabi and zbar.jar library.
Once you have imported cameraTest project in Eclipse, you should have a fully working code scanner named CameraTestActivity.
So to integrate the functionnality in your application follow these steps:
All required components are in CameraTestActivirty of course.
Copy/paste CameraTestActivity.java and CameraPreview.java from CameraTestActivity src to your proper src directory.
You would prefer to Copy/Paste the package, as you prefer.
Personnally, I renamed CameraTestActivity to QRCodeScanActivity
Copy/Paste all directoris in libs from CameraTestActivity to you proper libs directory.
Include the following part of AndroidManifest.xml in your proper AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" /> Here it's as you want, it should work also on non autofocus devices
... your already present declarations....
....
Copy/Paste the main.xml from layout i CameraTestActivity to your proper layout directory. CameraTestActivity will force portrait orientation during the scan.
Rename this file as you want; qrcodescan.xml if you want.
You have to declare that you use zbar.jar library.
Right-click on yout application name, select properties, click on Java Build Path, then click on Add JARs and go through yourAppName\libs to select zbar.jar.
You have to change 2 or 3 little things in the code.
In QRCodeScanActivity, previously CameraTest Activity:
public class QRCodeScanActivity extends Activity {
// declare a constant to use in the Intent created in your application to start he scan.
public final static int ACTIVITY_SCAN_CODE = 33;
.....
// Declare an intent to respond to your activity
Intent activityResult = new Intent();
// Not mandatory, you can add an intermediate String to return to your application
String textScanned;
......
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Put here the name you choosed in place of main.xml
setContentView(R.layout.qrcodescan);
....
// Later you will choose if you let the TextView and the Button or not. Personnaly, I removed it to just keep the test if (barcodescanned) and its content.
scanText = (TextView)findViewById(R.id.scanText);
scanButton = (Button)findViewById(R.id.ScanButton);
scanButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (barcodeScanned) {
barcodeScanned = false;
scanText.setText("Scanning...");
mCamera.setPreviewCallback(previewCb);
mCamera.startPreview();
previewing = true;
mCamera.autoFocus(autoFocusCB);
}
}
});
}
public void onPause() {
super.onPause();
releaseCamera();
}
...............
PreviewCallback previewCb = new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
Camera.Parameters parameters = camera.getParameters();
Size size = parameters.getPreviewSize();
Image barcode = new Image(size.width, size.height, "Y800");
barcode.setData(data);
int result = scanner.scanImage(barcode);
if (result != 0) {
previewing = false;
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
SymbolSet syms = scanner.getResults();
for (Symbol sym : syms) {
scanText.setText("barcode result " + sym.getData());
barcodeScanned = true;
// Here is what you have to add to return the result to your application
textScanned = sym.getData();
activityResult.putExtra("SCANNED", textScanned);
setResult(RESULT_OK, activityResult);
finish();
}
}
}
};
// Mimic continuous auto-focusing
AutoFocusCallback autoFocusCB = new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
autoFocusHandler.postDelayed(doAutoFocus, 1000);
}
};
}
In your application now:
Somewhere where you want to start the scan from; add:
Intent intent = new Intent(this, QRCodeScanActivity.class);
startActivityForResult(intent, QRCodeScanActivity.ACTIVITY_SCAN_CODE);
and you should have the necessary code to receive the result:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == QRCodeScanActivity.ACTIVITY_SCAN_CODE) {
String textScanned = intent.getStringExtra("SCANNED");
....
}
}
That should work vey very well.
Regards,

Embed zxing barcode scanner to the activity

I'm writing android application and my client requires a barcode scanner in it. They are really specific about it, so the layout they want is like this:
If a qr code found - it jumps to another window automatically. If manual pressed - you are asked to type manually and proceed with the rest of the app.
So basically I could embed zxing code to my app and add it to the activity but I don't want that and would like to have it as a separate app.
What I have at the moment is a separate activity called like this:
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.initiateScan();
I also tried this:
IntentIntegrator intentIntegrator = new IntentIntegrator(this);
Intent i = intentIntegrator.initiateCustomScan();
LocalActivityManager mgr = getLocalActivityManager();
Window w = mgr.startActivity("unique_per_activity_string", i);
View wd = w != null ? w.getDecorView() : null;
if(wd != null) {
scanButton.addView(wd);
}
But then I get java.lang.SecurityException:
03-19 12:22:55.890: E/AndroidRuntime(29394): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.menucard.barcode.scan/com.barcode.scan.ScanActivity}: java.lang.SecurityException: Requesting code from com.google.zxing.client.android (with uid 10139) to be run in process com.menucard.barcode.scan (with uid 10169)
Maybe someone has an idea how to add a separate app into my activity? Or other ways to accomplish this?
You can't embed an external app in another app via Intent unfortunately. The external app here needs to take over the whole screen, and is in landscape mode, for starters.
You should write your own app, but can reuse parts of Barcode Scanner in your app so that it's not entirely from scratch. Just please don't copy the AndroidManifest.xml file. I think it will also be clearly not confused with Barcode Scanner given the different UI. All that remains is to make sure you follow the terms of the Apache License (easy).
#MindaugasSvirskas, your last comment is exactly what I was about to post now:-) I have faced the same problem in the past, in several apps, and believe me, just make use of Intents, that's the way the whole Android system is designed, favouring intercommunication between apps. iOS programmers can easily integrate the scanning Zxing layout in their own layouts, but we are supposed to make use of intents, and I agree.

Accessing front camera of mobile using flash?

Recently I went thru the code for accessing the camera using flash ActionScript3 and I have tested the code in iMac machine, iPhone and Android.Now based on this, I am developing an application for Android which includes the accessibility of the front camera. Now my Problem is I dont know how to access the front camera? We should use some other code or should we specify which camera should be accessed? First of all, can we access the front camera thru flash?
I made a simple android app. Here is the code for selecting camera window
public class SelectCameraAlertAndroid extends StartAlertAndroid_design{
public function SelectCameraAlertAndroid() {
frontCameraButton.addEventListener(MouseEvent.CLICK, onFrontCamera);
backCameraButton.addEventListener(MouseEvent.CLICK, onBackCamera);
}
private function onFrontCamera(event:MouseEvent):void {
Model.model.camera = Camera.getCamera("1");
Model.model.cameraSelectedSignal.dispatch();
dispatchEvent(new Event("closeMe"));
}
private function onBackCamera(event:MouseEvent):void {
Model.model.camera = Camera.getCamera("0");
Model.model.cameraSelectedSignal.dispatch();
dispatchEvent(new Event("closeMe"));
}
}
Not true. You can access the front camera on Android.
The only problem is that you don't get to use the CameraUI(pretty sure).
var camera = Camera.getCamera("1");
camera.setMode(stage.stageWidth, stage.stageHeight, 30, true);
var video:Video = new Video(stage.stageWidth, stage.stageHeight);
video.attachCamera(camera);
addChild(video);
Note: This answer is outdated. Please refer to the other answers for updated information.
Currently, AIR only supports access to the primary camera on an Android device.
http://forums.adobe.com/thread/849983
Official documentation: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Camera.html#getCamera()
"On Android devices, you can only access the rear-facing camera."

Android setVideoEncodingBitRate() is not defined in package MediaRecorder

I am trying to the change the encoding bit rate of the video recording on Android using MediaRecorder.setVideoEncodingBitRate(int).
I looked in the android documentation and it states this method to set/change the bit rate but when I try to use this method I am getting setVideoEncodingBitrRate(int) is not defined in package MediaRecorder.
Why it is so?
I suggest you should check that which API version you are using
setVideoEncodingBitRate() just come on API v8 or Android 2.1
If you use version less than that it would not be available :D
Also you could use it like this
webCamRecorder = new MediaRecorder();
if (target_holder == null)
return;
webCamRecorder.setPreviewDisplay(target_holder.getSurface());
webCamRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
webCamRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
webCamRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
webCamRecorder.setAudioEncodingBitRate(196608);
webCamRecorder.setVideoSize(640, 480);
webCamRecorder.setVideoFrameRate(30);
webCamRecorder.setVideoEncodingBitRate(15000000);
webCamRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
webCamRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
webCamRecorder.setOutputFile("your location to save");
setVideoEncodingBitRate is an instance method, seems that you are trying to call it as a static method (MediaRecorder.setVideoEncodingBitRate(int)), instead, call it from the MediaRecorder object.
MediaRecorder mr = new MediaRecorder();
mr.setVideoEncodingBitRate(someint);
Also, did you import android.media.MediaRecorder ?

Categories

Resources