How do I integrate Vision API for barcode with google cardboard? - java

I am trying to integrate a qr code scanner into my google cardboard program. I have used the answer here to start a cardboard camera. I am encountering problems when I try to integrate the vision API for barcode reader into this. I need to start camera passing a texture to the vision API's camerasource portion.
final CameraSource cameraSource = new CameraSource
.Builder(this, barcodeDetector)
.setRequestedPreviewSize(640, 480)
.build();
How do I integrate the qr code scanner with vision API into my cardboard app?

Google has provided a supremely useful View for managing your CameraSource for preview and detection. You don't have to worry about managing the texture itself or determining the layout of the texture/surface in your view.
It's called CameraSourcePreview, and it's used to manage the operation of your CameraSource along with a SurfaceView.
You can use the CameraSourcePreview like any other view in your layout, for example in this full screen LinearLayout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/topLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
<com.google.android.gms.samples.vision.barcodereader.ui.camera.CameraSourcePreview
android:id="#+id/preview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.google.android.gms.samples.vision.barcodereader.ui.camera.CameraSourcePreview>
</LinearLayout>
And in your activity you set up your BarcodeDetector and CameraSource like normal (probably in your onCreate() method) and then (likely onResume()) start your CameraSourcePreview. It would look something like this:
if (mCameraSource != null) {
try {
mPreview.start(mCameraSource, mGraphicOverlay);
} catch (IOException e) {
Log.e(TAG, "Unable to start camera source.", e);
mCameraSource.release();
mCameraSource = null;
}
}
So really, you'll just be managing your CameraSource through your the CameraSourcePreview, but it handles a lot of the heavy lifting of the layout/preview/etc. so you don't have to. Check out the BarcodeCaptureActivity to see more about how the activity manages these components.
Check out the barcode-reader sample in the android vision sample projects for the complete project sample.
Hope that helps, cheers.

Related

How to add custom fixed layout on specific coordinates on Mapbox map using Java - 2023

The mapbox documentation have a lot of confusing deprecated classes and plugins to be used in an Android project.
I'm seeking for the best, latest and most appropriate way to implement custom layout on-top of MapboxView, while keeping in mind to not affect the map's performance, added views to dynamically zoom in and out on map zoom changes.
In short, add a custom view that behave same as the native Mapbox pins, but using a customized Android layout.
Here's an example:
I found this Mapbox documentation that refers to using something like ViewAnnotationManager and PointAnnotationManager but it's written in Kotlin.
Also tried to implement this approach but I couldn't find the above mentioned classes, here's the implemented libraries I have in my Gradle file:
implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-markerview-v9:0.4.0'
implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-annotation-v9:0.9.0'
Here's the layout I want to add on top of the map, it's a RippleBackground that contains an ImageView inside of it:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="250dp"
android:layout_height="250dp">
<com.skyfishjy.library.RippleBackground
android:id="#+id/layout_mapview_ripple"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
app:rb_color="#color/primary"
app:rb_radius="28dp"
app:rb_rippleAmount="3"
app:rb_duration="4000"
app:rb_scale="4">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/layout_mapview_user"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/design_white_circle"
android:padding="2dp"
android:scaleType="centerCrop"
android:layout_centerInParent="true"/>
</com.skyfishjy.library.RippleBackground>
</RelativeLayout>
And here's the method I currently have using MarkerView that receives latitude and longitude to add a View on top of the map. But this approach doesn't feel right and the View does not behave as expected when navigating through the map and zooming in/out
public void addUserRippleView(double latitude, double longitude){
View view = LayoutInflater.from(context).inflate(R.layout.layout_mapview_user_ripple, null);
view.setLayoutParams(new ViewGroup.LayoutParams(convertDpToPx(250), convertDpToPx(250)));
// User photo
CircleImageView mUserPhoto = view.findViewById(R.id.layout_mapview_user);
GlideUtil.loadProfilePicture(mSessionManager.getUserPhoto(), context, mUserPhoto);
// Ripple bg
RippleBackground rippleBackground = view.findViewById(R.id.layout_mapview_ripple);
rippleBackground.startRippleAnimation();
MarkerView markerView = new MarkerView(new LatLng(latitude, longitude), view);
mMarkerViewManager.addMarker(markerView);
}

How to include Java jar to C# Xamarin project (Picasso example), problem with ImageView android:src field

I am doing a java to c# transition, and need help.
in Visual Studio 2019 Pro, Android 9.0 (Pie), I am doing this example:
https://learn.microsoft.com/en-us/xamarin/android/platform/binding-java-library/binding-a-jar
The goal is later to convert my java libs to c# for my big project.
I follow the instructions to the letter, and when I come to the part where I need to create an ImageView, there is the problem. Once I create the ImageView, it has an android:src field, something like this (auto generated):
android:src="#drawable/icon"
The problem here, is when I remove that field, in the VS designer, the ImageView disappears and it does not matter if I have the c# code or not, even when I set the srec to:""
Bellow is literally all the code in MainActivity:
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Com.Squareup.Picasso;
namespace App3
{
[Activity(Label = "#string/app_name", Theme = "#style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
/***********************************************************************/
/**Picasso Code**/
ImageView imageView = FindViewById<ImageView>(Resource.Id.imageView1);
// Use the Picasso jar library to load and display this image:
Picasso.With(this)
.Load("http://i.imgur.com/DvpvklR.jpg")
.Into(imageView);
/**End Ff Picasso Code**/
/***********************************************************************/
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<ImageView
android:src="#drawable/icon" <!-- this line -->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView1" />
</LinearLayout>
The app does nothing, and in the emulator, the ImageView is empty.
And there is no error, or anything.
If you need anything more, please let me know.
I hope you can help me, if not, thank you for your time.
Edit 1:
It seems, the problem lies in the C# code.
If I add the android:src field, it still does not show up on the emulator, but when I remove the code (the Picasso code) than the image(drawable/icon) shows. Still no error!!
try this
<ImageView
android:src="#drawable/icon" <!-- this line -->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="50px"
android:minHeight="50px"
android:id="#+id/imageView1" />

Chromecast crashes when cast icon clicked

I am trying to add Chromecast function to my Android app. I've been following the code lab tutorial from Google's cast dev page, I've managed to get it working using there test app. But when I try to copy the code to from the test app to my own app i get the option to connect to my test device. But once it connects it just resets my Chromecast, then disconnects again.
This is my CastOptionProvider Class
public final class CastOptionsProvider implements OptionsProvider {
#Override
public CastOptions getCastOptions(Context context) {
return new CastOptions.Builder()
.setReceiverApplicationId(context.getString(R.string.app_id))
.build();
}
public List<SessionProvider> getAdditionalSessionProviders(Context context) {
return null;
}
}
This is my activities xml that I want to have Chromecast support
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="studios.p9p.harrop99.p9pdocumentaries.Actual_Genres.Taboo">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
The activities java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_taboo);
setupActionBar();
mCastContext = CastContext.getSharedInstance(this);
}
private void setupActionBar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Taboo");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
private MenuItem mediaRouteMenuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.browse, menu);
mediaRouteMenuItem = CastButtonFactory.setUpMediaRouteButton(getApplicationContext(), menu, R.id.media_route_menu_item);
return true;
}
I know the tutorial is not complete yet but by this point in the tutorial I can connect to my test casting device and a casting logo appears on my screen. But in my own app it resets my Chromecast and does not actually connect. Also I have a Google speaker that in the tutorial it will offer to connect to this. It does not even show in my app.
Here is a link to the tutorial I'm following.
Edit
Do I need a custom receiver? If so where would I upload it to?
fixed this issue, it was that i needed a custom Reciever adding to my server. it was crashing because i was trying to use the basic a basic Reciever from Googles dev page.
the basic reciever only allows for mp4 streams and direct video sources. But because im using youtube streams i had to use a package from github. this included a custom reciever to upload.
the basic tutorial i used can be found here
chromecast tutorial
and his github can be found here
github package and sample app
i also want to thank Pier Francescosoffritti for all the hard work he has put in to this

How can I display a Youtube video in Android app from desired video ID such that the ID is set from a server so that I can change the ID later?

I am new to Java and Android Studio.
I am currently working with Android Studio and I am able to load the YouTube video from the video ID, but in my app, I cant keep the video ID static I need to change the ID frequently without requiring users to download an app update. I have tried Firebase real-time database and remote config, but in both of them, the video lags because they download data asynchronously. What is the optimal solution to this problem?
Thank you for helping in advance.
Here is my YouTube video Fragment:
package shah.vatsal.kinitro
public class youtubefragment extends Fragment {
private static final String API_KEY =
"AIzaSyAXjBSS9NVAewJ2Z0Z86JrlHsJbJzoP_Ns";
private static String VIDEO_ID = "mMMerxh_12U";
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.youtube_videos, container,
false);
YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_layout, youTubePlayerFragment).commit();
youTubePlayerFragment.initialize(API_KEY, new OnInitializedListener() {
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
player.loadVideo(VIDEO_ID);
player.play();
}
}
#Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult error) {
// YouTube error
String errorMessage = error.toString();
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
Log.d("errorMessage:", errorMessage);
}
});
return v;
}
}
YouTube Layout:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/youtube_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<FrameLayout
android:id="#+id/youtube_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
I'm sure my suggestion doesn't follow the path you're trying to take, but you could easily achieve that by setting a webpage in which the video is put and then loading it from a WebView so as you could simply change the webpage and the video would be updated next time users run the app if you don't set a cache system or you just clear it whether you have it set.
It's way easier than trying to deal with Firebase development environment (which I find complicated), but you may deal with some lag while loading the page if the page isn't well optimized; however you could solve it today by simply setting a Tumblr space, for instance, and from time to time you could just change the link provided in order to showcase different videos.
Even if you want to show other elements in your activity besided the WebView, you could do it with some limitations or you could just insert what you like on the webpage itself (it depends mostly on your real needs).
Here is a Kotlin WebView sample in case you aren't familiar with it and here you can learn more about WebViews in general.

MediaPlayer / VideoView not playing video?

I'm new to android development, self taught so can expect a few errors here and there but none so irritating as this. I've looked over my code a thousand times, searched high and low across multiple websites, books and forums for an answer but I still get the same error so this is a last resort.
I just want to play a hardcoded path to a video in an activity, which is part of my video portfolio app. (The hardcoded path is just for testing, later I will call each video from the related button press, but only after I sort the player out!).
Here is my code:
String path = "android.resource://mysite/res/raw/video1";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videoplayer);
VideoView view = (VideoView) this.findViewById(R.id.vv);
MediaController controller = (MediaController) new MediaController(this);
controller.setMediaPlayer(view);
view.setVideoPath(path);
view.requestFocus();
view.start();
}
This snippet is inside my VideoPlayer class, where vv is the VideoView in the XML and video1 is the video to be played. The video is h.264 mp4, 1 minute long and 3mb in size and can be played normally through the default player.
XML:
<VideoView
android:id="#+id/vv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
The activity loads but returns the error "Can't play this video".
Any help I greatly appreciate.
VideoView view = (VideoView) this.findViewById(R.id.vv);
view.setVideoPath(path);
view.setMediaController(new MediaController(this));
view.requestFocus();
view.start();
Use like this. hope this will give you some solution.
According Android's official documentation Video decoding support for MP4(h.264) added from android 3.0+, so I think you are playing this video on version below 3.0. Try it on the device which has android os version 3.0+.
Try this..
video0=(VideoView)findViewById(R.id.vv);
video0.setMediaController(new MediaController(this));
video0.setVideoURI(Uri.parse("android.resource://" +getPackageName()+ "/" +R.raw.video1));
video0.requestFocus();
video0.start();
Did you try
MediaController mc = new MediaController(this);
mc.setAnchorView(vv);

Categories