How to implement scanning of barcode from gallery in android - android

Here is what i have tried.
I have implemented Zbar Scanner in android application in which I can scan barocde and get result.
I have implemented this in my android project. now I want to implement scanner which scans images(of course Bar code images) from gallery. I know this can be possible anyhow. check this link. It has barcode image scanning.
I have tried to search it out but failed. Please Help me out.

This is possible now with the new Barcode Scanning Apis available from Google Play Services 7.8 version. It has method to detect barcode passed as a bitmap.
Get path of image from gallery and convert it to bitmap and pass it like below:
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context)
.build();
if(barcode.isOperational()){
SparseArray<Barcode> sparseArray = barcodeDetector.detect(frame);
if(sparseArray != null && sparseArray.size() > 0){
for (int i = 0; i < sparseArray.size(); i++){
Log.d(LOG_TAG, "Value: " + sparseArray.valueAt(i).rawValue + "----" + sparseArray.valueAt(i).displayValue);
Toast.makeText(LOG_TAG, sparseArray.valueAt(i).rawValue, Toast.LENGTH_SHORT).show();
}
}else {
Log.e(LOG_TAG,"SparseArray null or empty");
}
}else{
Log.e(LOG_TAG, "Detector dependencies are not yet downloaded");
}
In your build.gradle file, include the following under dependencies section:
compile 'com.google.android.gms:play-services:7.8.+'
Following Manifest permissions are must:
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Meta data for google play services:
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
Meta data for first time install/run time dependencies to be downloaded for getting barcode detector operational.
<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="barcode" />
For detailed usage of this api, Refer Github Sample, follow Code Lab, Documentation.

Related

Changing Android hotspot settings

With the release of API level 26, my app's core functionality broke, this being, changing the users' hotspot setting within the application.
To get and set this configuration I am using the following functions from the WifiManager hidden api: getWifiApConfiguration and setWifiApConfiguration.
Method getWifiApConfiguration = wifiManager.getClass().getMethod("getWifiApConfiguration");
getWifiApConfiguration.invoke(wifiManager);
This is working with devices prior to Android O, but in this version I get the following error:
App not allowed to read or update stored WiFi Ap config (uid = 10168)
The permissions I have declared in the manifest are:
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.NETWORK_STACK"/>
<uses-permission android:name="android.permission.TETHER_PRIVILEGED" />
How can I do this with the latest APIs?
As of Android Oreo (26), a new permission check was added to the service implementation of the getWifiApConfiguration() method:
Snippet from WifiServiceImpl.java
/**
* see {#link WifiManager#getWifiApConfiguration()}
* #return soft access point configuration
* #throws SecurityException if the caller does not have permission to retrieve the softap
* config
*/
#Override
public WifiConfiguration getWifiApConfiguration() {
enforceAccessPermission();
int uid = Binder.getCallingUid();
// only allow Settings UI to get the saved SoftApConfig
if (!mWifiPermissionsUtil.checkConfigOverridePermission(uid)) {
// random apps should not be allowed to read the user specified config
throw new SecurityException("App not allowed to read or update stored WiFi Ap config "
+ "(uid = " + uid + ")");
}
mLog.trace("getWifiApConfiguration uid=%").c(uid).flush();
return mWifiStateMachine.syncGetWifiApConfiguration();
}
Digging into the code you will quickly find out that to successfully invoke this method your application must hold the android.permission.OVERRIDE_WIFI_CONFIG permission that is a system level protected permission:
Snippet from framework AndroidManifest.xml
<!-- #SystemApi #hide Allows an application to modify any wifi configuration, even if created
by another application. Once reconfigured the original creator cannot make any further
modifications.
<p>Not for use by third-party applications. -->
<permission android:name="android.permission.OVERRIDE_WIFI_CONFIG"
android:protectionLevel="signature|privileged" />
This means that your application needs to be signed by the platform key or be privileged to use this API.

Google Auth Error - com.google.android.gms.auth.UserRecoverableAuthException: NeedPermission

I'm studying Android programming and I'm having an error. It says:
"com.google.android.gms.auth.UserRecoverableAuthException:
NeedPermission"
Code
https://pastebin.com/kZivbfCm
Permissions
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Error
Caused by: com.google.android.gms.auth.UserRecoverableAuthException:
NeedPermission
I googled for whole day but not solved yet.
I'm using Youtube API and I have error for it.
Please help me.
Yes, added to Google API page.
http://i.imgur.com/nWP6TeR.png
And also YouTube API has ENABLED.
++ADDED
Class
private Context context1;
public YouTubePlaylistsLoader(Context context) {
super(context);
this.context1 = context;
}
and I tried
context1.startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
But It says cannot resolve that function.
I pressed Ctrl + SPACE and It only shows "startActivity(intent)"
From your code where
// I catched but I don't know how to handle.
// I tried startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); this but it gives me red line.
// It says "Cannot resolve method startActivityForResult"
// And also "Cannot resolve symbol REQUEST_AUTHORIZATION"
You need activity context to start activity for result. context.startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
Take a look in here

Getting 'Provider is not enabled | ProviderError{provider='network'}' on calling 'tracker.startListening();' even when WiFi is enabled

I'm using this library and trying to retrieve current location coordinates like this:
settings =
new TrackerSettings()
.setUseGPS(false)
.setUseNetwork(true)
.setUsePassive(true)
.setTimeBetweenUpdates(30 * 60 * 1000);
tracker = new LocationTracker(getBaseContext(), settings) {
#Override
public void onLocationFound(Location location) {
// Do some stuff
currentLatDouble = location.getLatitude();
currentLngDouble = location.getLongitude();
}
#Override
public void onTimeout() {
}
};
tracker.startListening();
but, I'm getting this error:
W/LocationTracker: Provider (network)
fr.quentinklein.slt.ProviderError: Provider is not enabled | ProviderError{provider='network'}
Isn't WiFi a network provider or do I need to write some code related to LocationManager too?
Please let me know what is wrong here.
Added the following permission in manifest file
<uses-permission android:name="android.permission.INTERNET" />
Allows applications to open network sockets.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Allows an app to access approximate location.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Allows an app to access precise location.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Allows applications to access information about networks.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Allows applications to access information about Wi-Fi networks.
Additionally
<uses-feature
android:name="android.hardware.location.network"
android:required="false" />
<uses-feature
android:name="android.hardware.location.gps"
android:required="false" />

Camera not working on actual device

I've integrated Camera within my app. The below codes are working fine when I'm running on emulator and I've selected webcam as my camera in ADB. But when I'm running on Actual device like Nexus 7 then my app is not able to detect camera. camera is working fine in this tablet independently but not within my app.
CameraActivity.java
//I've imported hardware camera class
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
// Some activity code
if (!getPackageManager().
hasSystemFeature(PackageManager.FEATURE_CAMERA)) { // Issue is coming from here.
Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG)
.show();
} else {
cameraId = findFrontFacingCamera();
if (cameraId < 0) {
Toast.makeText(this, "No front facing camera found.",
Toast.LENGTH_LONG).show();
} else {
camera = Camera.open(cameraId);
}
}
Mainfest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
Make changes here
try to check:
hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)
Instead:
hasSystemFeature(PackageManager.FEATURE_CAMERA)
Nexus 7 having only front-facing camera.
Android camera selection algorithm defaults to the rear camera.

Android Add <uses-feature> in Manifest

I am pretty confused where to add the
uses-feature
tag in the manifest.
I am using the camera in my app. I added permission but I'm confused where to add features in order to use front facing camera. Can you help?
Add this under <manifest> tag, like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lalllala">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-feature android:name="android.hardware.camera" />
<application android:icon="#drawable/icon" android:label="lalla" android:debuggable="true">
</application>
</manifest>
<uses-feature> - Declares a single hardware or software feature that is used by the application.
The purpose of a declaration is to inform any external entity of the set of hardware and software features on which your application depends. The element offers a required attribute that lets you specify whether your application requires and cannot function without the declared feature, or whether it prefers to have the feature but can function without it. Because feature support can vary across Android devices, the element serves an important role in letting an application describe the device-variable features that it uses.
read for more
Below is sample code to access Device Front Camera
public Camera openFrontFacingCamera() {
int cameraCount = 0;
Camera ffCam = null;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
// Find the total number of cameras available
cameraCount = Camera.getNumberOfCameras();
// Find the ID of the CAMERA_FACING_FRONT & open it
for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
ffCam = Camera.open(camIdx);
} catch (RuntimeException e) {
Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
}
}
}
return ffCam;
}
Need following permissions
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />
For more please read Google android developer API doc Camera, Camera.CameraInfo
Add this under manifest tag:
<!-- Request the camera permission -->
<uses-permission
android:name="android.permission.CAMERA" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
write tags order like this
<manifest>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application>
</application>
</manifest>

Categories

Resources