Display random images from USB on Raspberry Pi

0 votes

I have a Raspberry Pi 2 and I've managed to create my first IoT project, where I load an image by just pressing a button on my device and my code picks a random image to display from a given set of available images. But, there are still certain things I need that I haven't been able to figure out yet. Like say, 

  • How I can scan a USB stick from the Raspberry Pi?
  • How I can load an image from a file(I've only managed to do it from an embedded resource)?
  • How can I close the app(I assumed App.exit would do, but it doesn't seem like it)?

Although this is just a personal little project that I was trying to play around, my entire idea was to create a sort of digital photo frame that could randomly display different images from a USB stick. Now, I'm feeling a little stuck with all the above-mentioned details. But, if only I could get them all to work with the button press, I could then easily drop in a timer and keep it running all the time.

The following is my code(I've added comments to explain all the things that I have done so far):

' The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
''' <summary>
''' An empty page that can be used on its own or navigated to within a Frame.
''' </summary>

Public NotInheritable Class MainPage
    Inherits Page
    Dim random As Random = New Random()

    Private Sub ClickMe_Click(sender As Object, e As RoutedEventArgs)
        'Dim DirectoryPath As String = "F:/VB.Net files/Images/"
        'Dim finfo As FileInfo = New FileInfo(GetRandomImageFilePath(DirectoryPath))
        'Dim filename As String = finfo.Name.Replace(finfo.Extension, "")
        'BBPLogo.Source = New BitmapImage(New Uri("file:///" & finfo.FullName, UriKind.Relative))
        BBPLogo.Source = New BitmapImage(New Uri("ms-appx:///Assets/Bowral-Bricks.png", UriKind.Absolute))
    End Sub

    Private Sub ExitButton_Click(sender As Object, e As RoutedEventArgs) Handles ExitButton.Click

    End Sub

    Public Function GetRandomImageFilePath(ByVal folderPath As String) As String
        Dim files() As String = Directory.GetFiles(folderPath, "*.png")
        Return files(random.Next(0, files.Length))
    End Function

End Class

And, the following is the XAML:

<Page
    x:Class="PiTest01.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:PiTest01"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" Height="480" Width="800">

    <Grid>
        <Grid.Background>
            <ImageBrush Stretch="Fill"/>
        </Grid.Background>
        <Image x:Name="BBPLogo" HorizontalAlignment="Left" Height="480" VerticalAlignment="Top" Width="800" RenderTransformOrigin="1.163,0.596" Stretch="UniformToFill"/>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Margin="78,18,436,164" Height="178">
            <Button x:Name="ClickMe" Content="Click Me!"  Margin="10" HorizontalAlignment="Center" Click="ClickMe_Click"/>
            <Button x:Name="ExitButton" Content="Exit" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="126"/>
        </StackPanel>
    </Grid>
</Page>
Nov 28, 2018 in IoT (Internet of Things) by Bharani
• 4,660 points

edited Nov 28, 2018 by Bharani 1,008 views

1 answer to this question.

0 votes

Hey, it's a cool idea! And, since it seemed so doable, I tried it with my Raspberry Pi as well. 

Here's how you could find and scan for image files on a USB stick:

Begin by adding the "Removable Storage" capability to your app: 

  • open Package.appxmanifest 
  • navigate to the Capabilities tab,
  • and, ensure the "Removable Storage" option is checked. 

This will basically allow your app to access files from USB devices. 

But, because Universal Applications can only enumerate or read or modify file types that have been declared explicitly in the manifest under "File Type Associations", once again:

  • open Package.appxmanifest, 
  • navigate to the Declarations tab,
  • select "File Type Associations", 
  • and, hit Add. 

Now, just fill in the fields on the right-hand pane according to all the file types that your app wants to support(check MSDN for more details).

Then, you'll be able to enumerate removable drives and their sub-files/folders as shown in the these examples. Let's say, for instance, if you wanted to enumerate top-level files (I'm using C#):

var drives = await Windows.Storage.KnownFolders.RemovableDevices.GetItemsAsync();

foreach (var drive in drives)
{
    var files = await drive.GetFilesAsync();
}

Note: Only sub-folders and explicitly associated file types will show up.

Here's how you could load an image from a file:

Since you can't load a file directly from a file:// URI, you could go via a StorageFile and stream instead, something like this for example:

var file = await StorageFile.GetFileFromPathAsync(@"E:\test.png");

using (var stream = await file.OpenAsync(FileAccessMode.Read))
{
    var bmp = new BitmapImage();
    await bmp.SetSourceAsync(stream);
    ImageControl.Source = bmp;
}

For exiting the application:

App.Current.Exit(); seemed to work just fine for exiting the app in my case. It was followed by that weird progress spinner symbol that usually indicates messages like "Please wait while your device is being set up" or "Please wait while we save your progress" and that annoyingly lingered on for a while.

answered Nov 28, 2018 by nirvana
• 3,130 points

Related Questions In IoT (Internet of Things)

0 votes
1 answer

How to trigger Python script on Raspberry Pi from Node-Red

Node-RED supplies an exec node as part ...READ MORE

answered Sep 14, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
3,055 views
0 votes
1 answer

Accessing Kinect data on the Raspberry Pi

Yes, you can. The Kinect has a ...READ MORE

answered Aug 14, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
2,943 views
0 votes
1 answer

Sending more than 8 byte through UART on Raspberry Pi using Android Things!

So, you can send 234212441325454543595674859764 in ASCII, ...READ MORE

answered Aug 25, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,032 views
0 votes
1 answer

Running two processes on Raspberry Pi at the same time

I see no reason why it shouldn't ...READ MORE

answered Aug 28, 2018 in IoT (Internet of Things) by DataKing99
• 8,240 points
1,777 views
0 votes
1 answer

Naming my deployed application

Look in your Package.appxmanifest -- the name ...READ MORE

answered Sep 10, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
339 views
0 votes
1 answer

.NET Core on Raspbian/ARM?

.NET Core 2.1 supports Raspberry Pi. Here is ...READ MORE

answered Aug 31, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
757 views
+1 vote
1 answer

RegEx to remove XML tags and their content

I presume you want to drop the ...READ MORE

answered Feb 13, 2019 in IoT (Internet of Things) by Shubham
• 13,490 points
5,786 views
0 votes
1 answer

Setting-up a RFID RC522 chip in Raspberry Pi?

First, let me congratulate you on buying ...READ MORE

answered Jul 10, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,409 views
0 votes
1 answer
0 votes
1 answer

Setting time on Raspberry Pi using just PowerShell

The Enter-PSSession is for interactive sessions, so ...READ MORE

answered Aug 10, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
770 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP