I am trying to build an app that reads a QRCode from an IDCard that everyone on my company will use.
I would like to use the front camera as default when I run the app. I managed to do it with the rear camera but ideally I don't want to have to use a button to switch.
MainActivity:
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private static final int REQUEST_CAMERA = 1;
private ZXingScannerView mScannerView;
Passageiros mPassageiro; //
Collection<Passageiros> listaPassageiros;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mScannerView = new ZXingScannerView(this){
#Override
protected IViewFinder createViewFinderView(Context context) {
return new CustomZXingScannerView(context);
}
};
List<BarcodeFormat> formats = new ArrayList<>();
formats.add(BarcodeFormat.QR_CODE);
setContentView(mScannerView);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
if (checkPermission()) {
Toast.makeText(getApplicationContext(), "Permission already granted", Toast.LENGTH_LONG).show();
} else {
requestPermission();
}
}
}
...
}
How can I do this?
Looking at the sources of ZXing library
ZXingCameraView is extending BarcodeScannerView, which has a private CameraWrapper which you can set with setupLayout method. CameraWrapper allows you to initialize com.android.Camera.
And how to choose necessary out of available cameras is shown in that question: How do I open the "front camera" on the Android platform?
Edit: Actually there is even a ZXing CameraUtils class selecting camera on the back of device. Just change it to return id of front camera and pass that id to startCamera(frontCameraId) on your ZXingCameraView.
Related
i need UseButton in my app to link uber app.For this i have implemented the following code.In useButton Developer site, it has been said to submit for review. while filling the review form, its asking screenshot with a working button. but my button in the screen is not visible. how can it be working button.
please help me.
public class MainActivity2 extends AppCompatActivity {
RelativeLayout rootLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rootLayout = (RelativeLayout) findViewById(R.id.rootlayout);
final ButtonDropin buttonDropin = new ButtonDropin(MainActivity2.this);
buttonDropin.setButtonId("btn-11cd09c63aaa7c02");
buttonDropin.setBackgroundResource(R.drawable.ic_app_button);
buttonDropin.setTextSize_Button(getResources().getDimensionPixelSize(R.dimen.my_text_size));
ButtonContext contexts = null;
try {
final RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
buttonDropin.setLayoutParams(layoutParams);
rootLayout.addView(buttonDropin);
contexts = ButtonContext.withSubjectLocation(new Location("Button HQ", 11.9362587, 79.8268325));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
final android.location.Location userLocations = new LocationProvider(this).getBestLocation();
if (userLocations != null) {
contexts.setUserLocation(new Location(userLocations));
}
}catch (Exception e){
Toast.makeText(MainActivity2.this,"Exception :"+e,Toast.LENGTH_LONG).show();
}
buttonDropin.prepareForDisplay(contexts, new ButtonDropin.Listener() {
#Override
public void onPrepared(final boolean isReady) {
Toast.makeText(MainActivity2.this,"IsReady:"+isReady,Toast.LENGTH_LONG).show();
}
#Override
public void onClick(ButtonDropin buttonDropin) {
Toast.makeText(MainActivity2.this,"buttonDropin onclick",Toast.LENGTH_LONG).show();
}
});
}}
Buttons that need to be reviewed will still render. Also, Buttons will only render if there is inventory to display (for this example, will there be Ubers to take from where the user is to where the user is going, which set in the Context).
First, add the Button SDK to your app's build.gradle file
compile 'com.usebutton:android-sdk:5+'
Then, add Button to your AndroidManifest.xml file
<application
<activity
<!-- your activities -->
</activity>
<!--Button SDK-->
<meta-data android:name="com.usebutton.applicationid" android:value="YOUR_BUTTON_APP_ID"/>
</application>
Then, import the SDK at the top of the Activity and start it. This should be called when your application class is created.
import com.usebutton.sdk.Button;
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
Button.getButton(this).start();
}
}
Then, add the Button to your view:
<com.usebutton.sdk.ButtonDropin
android:id="#+id/main_dropin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
And then load the action for your Button in your Activity file:
final ButtonDropin buttonDropin = (ButtonDropin) findViewById(R.id.main_dropin);
// Set location to go to
final Location location = new Location("LOCATION_NAME", LATITUDE, LONGITUDE);
// Create Button Context
final ButtonContext buttonContext = ButtonContext.withSubjectLocation(location);
// Get Button Action and display if there is inventory
Button.getButton(this).getAction("YOUR_BUTTON_ID", buttonContext, "my-pub-ref", new Button.ActionListener() {
#Override
public void onAction(final AppAction action) {
// Display Button
buttonDropin.prepareWithAction(action);
}
#Override
public void onNoAction() {
// Don't display Button
buttonDropin.setVisibility(View.GONE);
}
});
I need to show list of all possible displays , and after that select one of them to connect with miracast. Here is my code:
private Context context;
private DisplayManager mDisplayManager;
private WifiP2pManager wifiP2pManager;
private ArrayList<WifiP2pDevice> devices;
private WifiP2pManager.Channel channel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
context = this;
devices = new ArrayList<WifiP2pDevice>();
buttonScan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startScan();
}
});
mDisplayManager = (DisplayManager)this.getSystemService(Context.DISPLAY_SERVICE);
wifiP2pManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
channel = wifiP2pManager.initialize(this, getMainLooper(), new WifiP2pManager.ChannelListener() {
public void onChannelDisconnected() {
channel = null;
Logger.makeLog("onChannelDisconnected");
}
});
}
WifiP2pManager.PeerListListener myPeerListListener = new WifiP2pManager.PeerListListener() {
#Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {
// Out with the old, in with the new.
devices.clear();
devices.addAll(peerList.getDeviceList());
Logger.makeLog("devices size " + devices.size());
Toast.makeText(context, "devices size " + devices.size(), Toast.LENGTH_LONG).show();
if (devices.size() == 0) {
Logger.makeLog("No devices found");
return;
}
}
};
private void startScan() {
wifiP2pManager.requestPeers(channel, myPeerListListener);
}
After compiling this code, i press buttonScan, and it show, that 0 displays in area. But one display was near me. But then i connected to the display (TV), entered my app, pressed buttonScan, and it shows me, that 2 displays around me (TV and display of mobile). But its bad, i need to scan all possible displays to connect BEFORE connect... So, what am I doing wrong?
How can i use TextToSpeech functionality without having any interface ? I am developing a app for visually Impaired persons , i just need to generate a voice alert, and the text (alert will be hard coded). Can anyone help me with this ? I am getting error on this TextToSpeech.OnInitListener ..... Is interface is necessory for this ?
Try this one:
public class YourActivity extends Activity {
private TextToSpeech tts;
public static void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your);
tts = new TextToSpeech(YourActivity.this, InitListener);
}
private TextToSpeech.OnInitListener InitListener = new TextToSpeech.OnInitListener() {
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int res = tts.setLanguage("en", "US", "");
if (res >= TextToSpeech.LANG_AVAILABLE) {
String text = "Input text what you want to say.";
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
}
}
}
This app doesn't need UI, and will make voice just once when app is executed.
I am creating a flashlight app. In my code, if camera(hardware) is there, it should show an alert, but it does not show the alert. My code is as below:
if (!isCameraFlash) {
showNoCameraAlert();
} else {
camera = Camera.open();
params = camera.getParameters();
}
but i get an error on
showNoCameraAlert();
Is this an invalid tag? or the tutorial i am using is old? I am using android studio. Here is my full Java code:
public class MainActivity extends ActionBarActivity {
private Camera camera;
ImageButton flashlightSwitchImg;
private boolean isFlashlightOn;
Parameters params;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flashlightSwitchImg = (ImageButton) findViewById(R.id.flashlightSwitch);
boolean isCameraFlash = getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!isCameraFlash) {
showNoCameraAlert();
} else {
camera = Camera.open();
params = camera.getParameters();
}
}
}
Any help will be appreciated. Thanks :)
You are calling the showNoCameraAlert() method. You need to add it to your MainActivity (that is the file where you put the code in.) So add it like this:
private void showNoCameraAlert(){
Toast.makeText(getApplicationContext(), "Camera flashlight not available in this Android device!", Toast.LENGTH_SHORT).show();
}
The full code for your MainActivity:
package com.example.administrator.flashlight;
import ...;
public class MainActivity extends ActionBarActivity {
private Camera camera;
ImageButton flashlightSwitchImg;
private boolean isFlashlightOn;
Parameters params;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flashlightSwitchImg = (ImageButton) findViewById(R.id.flashlightSwitch);
//check if phone has a flashlight
boolean isCameraFlash = getApplicationContext().getPackageManager() .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)
if (!isCameraFlash) {
//show error ( showNoCamereAlert() is below)
showNoCameraAlert();
} else {
//open the camera
camera = Camera.open();
params = camera.getParameters();
}
}
private void showNoCameraAlert(){
//Show Error toast
Toast.makeText(getApplicationContext(), "Camera flashlight not available in this Android device!", Toast.LENGTH_SHORT).show();
}
}
I'm starting to think that you didn't implement the showNoCameraAlert() method.
Try adding something like this to your MainActivity class:
private void showNoCameraAlert(){
Toast.makeText(getApplicationContext(), "There's no camera on the device or it doesn't have a flash", Toast.LENGTH_SHORT).show();
}
I am successfully integrate YouTube Android Player API for my apps. I am curious to know about which file is rendered in the player. Every single video file may have several formats like 320dpi,720dpi & etc.
If my users, is in lower bandwidth; can i choose the file format or the API automatically detects which version would be played in that situation and vice-verse. My code:
public class YoutubeVideoActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener, YouTubePlayer.OnFullscreenListener {
Activity activity = YoutubeVideoActivity.this;
public static final String API_KEY = "AIzaSyDN6Q9Pv4seQZqIcjB*********Po5k";
// public static final String VIDEO_ID = "psY0Botpi84";
public String new_id;
private boolean fullscreen;
private YouTubePlayerView playerView;
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
// Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_youtube_video);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Intent intent = getIntent();
String video_link = intent.getExtras().getString("video_link");
try {
new_id = video_link.substring("http://www.youtube.com/watch?v="
.length());
if (new_id.equals("")) {
IndepententTVUtils.showCustomAlert(activity,
"Data is not availble. Press back key",
R.drawable.ic_new_launcher);
} else {
playerView = (YouTubePlayerView) findViewById(R.id.youtubeplayerview);
playerView.initialize(API_KEY, this);
}
} catch (Exception e) {
IndepententTVUtils.showCustomAlert(activity,
"Data is not availble", R.drawable.ic_new_launcher);
}
}
#Override
public void onInitializationFailure(Provider arg0,
YouTubeInitializationResult arg1) {
Toast.makeText(getApplicationContext(),
"To See this Video, Install Latest YouTube Application",
Toast.LENGTH_LONG).show();
}
#Override
public void onInitializationSuccess(Provider arg0, YouTubePlayer player,
boolean wasRestored) {
player.setOnFullscreenListener(this);
if (!wasRestored && new_id != null) {
player.cueVideo(new_id);
}
}
#Override
public void onFullscreen(boolean isFullscreen) {
fullscreen = isFullscreen;
}
}
The player sets video quality automatically according to the users bandwidth or internet speed, if it was not so there would be an option specifying the setPlaybackQuality in player.
There are only restricted options to change the player settings that are
here