Detecting Front and Back camera in windows 8 tabs - windows-8

I am developing a metro style app using c# and xaml. For a particular task i need to detect which cam(front or back) is currently capturing. Is there any way to detect front cam or back cam in winrt. Please help me.

Using indexes on the DeviceInformationCollection won't be a reliable solution:
Sometimes front camera will be index 0 and sometimes 1, after testing on the surface 2 a few times it seems to be kind of random.
User could use the USB port to connect an other webcam so you could end up with more than 2 items in your collection without any clue which camera is which index.
Having the same problem as you this is how I solved it:
// Still need to find all webcams
DeviceInformationCollection webcamList = await eviceInformation.FindAllAsync(DeviceClass.VideoCapture)
// Then I do a query to find the front webcam
DeviceInformation frontWebcam = (from webcam in webcamList
where webcam.EnclosureLocation != null
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front
select webcam).FirstOrDefault();
// Same for the back webcam
DeviceInformation backWebcam = (from webcam in webcamList
where webcam.EnclosureLocation != null
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
select webcam).FirstOrDefault();
In this exemple I used Linq queries but it works the same with a foreach on "webcamList".
Just look on each DeviceInformation the .EnclosureLocation.Panel property, which is a Windows.Devices.Enumeration.Panel enum. The rest is obvius, Front for the front camera, Back for the back one.
Be also carefull to check if .EnclosureLocation is null or not, using an USB webcam it seems to be null most of the time.

You can use this code.
DeviceInformationCollection videoCaptureDevices = await eviceInformation.FindAllAsync(DeviceClass.VideoCapture);
If the videoCaptureDevices count is zero ,there is no camera attached.
And if camera count is 2 , then there will be both Front & back cameras.
If you initializing camera operation with videoCaptureDevices [0], that will be using Front Camera, and will be Back Camera if it is using videoCaptureDevices [1] .

Related

Check if there a usb/AC connection

I'm developing an universal windows phone application which checks if there a connection via usb or AC charging.
I used this code:
var deviceInfo = await DeviceInformation.FindAllAsync(Battery.GetDeviceSelector());
var aggBattery = Battery.AggregateBattery;
var report = aggBattery.GetReport();
report.Status.ToString();
It works well for some devices.
There are few devices (devices which got upgraded from WP8 to WP10) that takes them 30 seconds to detect the "charging mode".
I want to know if there a way to detect a usb connection?
To detect USB device, we can use a DeviceWatcher object to detect when the device is added to or removed from the system.
For Windows 10 Universal app, here is a good sample: Device enumeration sample
or get the battery level?
The Battery class does not provide method to get the battery level.
As a workaround, we can use BatteryReport.RemainingCapacityInMilliwattHours to get the remaining power capacity of the battery, in milliwatt-hours. Also we can use BatteryReport.FullChargeCapacityInMilliwattHours to get the fully-charged energy capacity of the battery. Then we can use BatteryReport.RemainingCapacityInMilliwattHours/BatteryReport.FullChargeCapacityInMilliwattHours to get the battery level.
For example:
var deviceInfo = await DeviceInformation.FindAllAsync(Battery.GetDeviceSelector());
var aggBattery = Battery.AggregateBattery;
var report = aggBattery.GetReport().RemainingCapacityInMilliwattHours;
var report3 = aggBattery.GetReport().FullChargeCapacityInMilliwattHours;
MyText.Text = (report / (double)report3).ToString();

Get USB disk drive letter by device path or handle

My goal is to write a c-dll (compiled with MinGW) that is able to search for certain models of USB sticks connected to the computer and deliver the serial number, the vendor ID, the product ID and the drive letter.
I have searched on the internet for several hours know but could not find an approach that works for me.
I am using the Setup Api to get a list of all connected USB devices. For each USB device I get a path that looks like this:
\?\usb#vid_048d&pid_1172#00000020370220#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
From that string I can get the vendor ID, product ID and the serial number I am looking for.
My problem is now to determine the drive letter of the USB drive that is related to this device path.
During my internet research I found the following approach multiple times (for example here http://oroboro.com/usb-serial-number/):
Once the device path is found, the USB drive must be opened by CreateFile. The handle returned by that function can be used to get the device number by function DeviceIOControl with IOCTL_STORAGE_GET_DEVICE_NUMBER.
After that, the CreateFile function could be used to open each drive letter (starting from a:) and try to get the device number the same way like described above. Once the same device number is found again, the relation between device path and drive letter is made.
My Problem is that the IOCTL_STORAGE_GET_DEVICE_NUMBER call is not working. The DeviceIOControl function returns error code 50 which means "The request is not supported".
I am not able to create a link between the device path of a USB stick and the drive letter. I have tried several IOCTL_STORAGE and IOCTL_VOLUME calls but none worked for the USB sticks I tried.
I also read in another Forum that people had problems with the results of the DeviceIOControl function. It was returning the desired result on some PCs while it was making trouble on others.
Is there another way of achieving my goal?
I already had a look into the registry where I can also find the desired data. But again I had the problem to create the connection between device path and drive letter.
I would not like to use the WMI. I have read that it is still not really supported by MinGW.
I have a implementaion for all this with C# where it is really easy to get the desired information, but now I also need one that is created with unmanaged code and can be used to replace a c-dll also included in Delphi projects.
I would appreciate any suggestions for a solution to my problem.
Best regards,
Florian
And here the code if someone is interested. The position with this comment "//HERE IS WHERE I WOULD LIKE TO GET THE DEVICE NUMBER!!!" is where the request of the device number would be used if it would work.
typedef struct ty_TUSB_Device
{
PSP_DEVICE_INTERFACE_DETAIL_DATA deviceDetailData;
char devicePath[300];
}TUSB_Device;
int
GetUSBDevices (TUSB_Device *devList[], int size)
{
HANDLE hHCDev;
HDEVINFO deviceInfo;
SP_DEVICE_INTERFACE_DATA deviceInfoData;
ULONG index;
ULONG requiredLength;
int devCount = 0;
//SP_DEVINFO_DATA DevInfoData;
// Now iterate over host controllers using the new GUID based interface
//
deviceInfo = SetupDiGetClassDevs((LPGUID)&GUID_DEVINTERFACE_USB_DEVICE,
NULL,
NULL,
(DIGCF_PRESENT | DIGCF_DEVICEINTERFACE));
if (deviceInfo != INVALID_HANDLE_VALUE)
{
deviceInfoData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
for (index=0;
SetupDiEnumDeviceInterfaces(deviceInfo,
0,
(LPGUID)&GUID_DEVINTERFACE_USB_DEVICE,
index,
&deviceInfoData);
index++)
{
SetupDiGetDeviceInterfaceDetail(deviceInfo,
&deviceInfoData,
NULL,
0,
&requiredLength,
NULL);
//allocate memory for pointer to TUSB_Device structure
devList[devCount] = malloc(sizeof(TUSB_Device));
devList[devCount]->deviceDetailData = GlobalAlloc(GPTR, requiredLength);
devList[devCount]->deviceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
SetupDiGetDeviceInterfaceDetail(deviceInfo,
&deviceInfoData,
devList[devCount]->deviceDetailData,
requiredLength,
&requiredLength,
NULL);
//open the usb device
hHCDev = CreateFile(devList[devCount]->deviceDetailData->DevicePath,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
// If the handle is valid, then we've successfully found a usb device
//
if (hHCDev != INVALID_HANDLE_VALUE)
{
strncpy(devList[devCount]->devicePath, devList[devCount]->deviceDetailData->DevicePath, sizeof(devList[devCount]->devicePath));
//HERE IS WHERE I WOULD LIKE TO GET THE DEVICE NUMBER!!!
CloseHandle(hHCDev);
devCount++;
}
//GlobalFree(devList[devCount]->deviceDetailData);
}
SetupDiDestroyDeviceInfoList(deviceInfo);
}
return devCount;
}
I found out what my problem was. From what I read on the internet it seems there where other people having the same problems like me, so I will post my solution.
The whole point is that there are obviously different path values one can obtain for a USB device using the SetupApi. All path values can be used to get a handle to that device, but there are obviously differences about what can be done with the handle.
My failure was to use GUID_DEVINTERFACE_USB_DEVICE to list the devices. I found out that when I use GUID_DEVINTERFACE_DISK, I get a different path value that lets me request the device number. That way I am able to get the link to the drive letter.
That path value obtained with GUID_DEVINTERFACE_DISK also contains the serial number but not the vendor and product IDs. But since both path values do contain the serial, it is no problem to get them both and build the relation.
I tested the code with Windows XP, 7 and 8 and it works fine. Only the FileCreate code of the code sample above must be adjusted (replace GENERIC_WRITE by 0). Otherwise Administrator rights or compatibility mode are required.
I did not try to find out what these different GUID values really stand for. Someone with a deeper knowledge in this area could probably provide a better explanation.
Best regards,
Florian

How to get notifications when the headphones are plugged in/out? Mac

I'd like to get notified when headphones are plugged in or out in the headphone jack.
I've searched around for this on stackoverflow but I can't seem to find what I'm looking for for the Mac, I can only find for iOS.
So, do you have any ideas on how to perform this? What I want to do with this is: when headphones are plugged out I want to programmatically pause iTunes (iOS-like feature).
Thank you!
You can observe changes using the CoreAudio framework.
Both headphones and the speakers are data sources on the same audio output device (of type built-in). One of both will be on the audio device based on headphones being plugged in or not.
To get notifications you listen to changes of the active datasource on the built-in output device.
1. Get the built-in output device
To keep this short we'll use the default output device. In most cases this is the built-in output device. In real-life applications you'll want to loop all available devices to find it, because the default device could be set to a different audio device (soundflower or airplay for example).
AudioDeviceID defaultDevice = 0;
UInt32 defaultSize = sizeof(AudioDeviceID);
const AudioObjectPropertyAddress defaultAddr = {
kAudioHardwarePropertyDefaultOutputDevice,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
AudioObjectGetPropertyData(kAudioObjectSystemObject, &defaultAddr, 0, NULL, &defaultSize, &defaultDevice);
2. Read its current data source
The current datasource on a device is identified by an ID of type UInt32.
AudioObjectPropertyAddress sourceAddr;
sourceAddr.mSelector = kAudioDevicePropertyDataSource;
sourceAddr.mScope = kAudioDevicePropertyScopeOutput;
sourceAddr.mElement = kAudioObjectPropertyElementMaster;
UInt32 dataSourceId = 0;
UInt32 dataSourceIdSize = sizeof(UInt32);
AudioObjectGetPropertyData(defaultDevice, &sourceAddr, 0, NULL, &dataSourceIdSize, &dataSourceId);
3. Observe for changes to the data source
AudioObjectAddPropertyListenerBlock(_defaultDevice, &sourceAddr, dispatch_get_current_queue(), ^(UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses) {
// move to step 2. to read the updated value
});
Determine the data source type
When you have the data source id as UInt32 you can query the audio object for properties using a value transformer. For example to get the source name as string use kAudioDevicePropertyDataSourceNameForIDCFString. This will result in the string "Internal Speaker" or "Headphones". However this might differ based on user locale.
An easier way is to compare the data source id directly:
if (dataSourceId == 'ispk') {
// Recognized as internal speakers
} else if (dataSourceId == 'hdpn') {
// Recognized as headphones
}
However I couldn't find any constants defined for these values, so this is kind of undocumented.
I was looking for a similar solution and found AutoMute in the app store. It works well.
I'm also working on some scripts of my own, and wrote this script to test if headphones are plugged in:
#!/bin/bash
if system_profiler SPAudioDataType | grep --quiet Headphones; then
echo plugged in
else
echo not plugged in
fi

What is the easiest way to get track data off a simple USB HID magnetic card reader?

I need to get Track 1 and Track 2 data off magnetic cards and send them over the network to a waiting server. What is an easy way to get the track data from a USB HID magnetic card reader?
In case it helps, I have a MAGTEK Mini Swipe Magnetic Strip Reader (part no. 21040140)
I'm OS agnostic -- a solution for Windows, Mac or Linux would be great. Preferably no .NET, but if that's the easiest way I'll go for it.
What do you all think?
Thanks!
Every card reader I've seen has had a keyboard emulator, so you swipe the card and it sends characters through the keyboard buffer. Looks like this one also does that (documentation : http://www.magtek.com/documentation/public/99875206-16.01.pdf)
Page 14 describes the data sent after a swipe, which is again, fairly standard across card readers:
[Tk1 SS] [Tk1 Data] [ES] [Tk2 SS] [Tk2 Data] [ES] [Tk3 SS] [Tk3 Data] [ES] [CR]
So your track one data starts with % and ends with ?
Track two data starts with ; and ends with ?
I noticed the question was tagged credit-card though, so it would be worth making sure you know the consequences of sending raw card-data across a network (even an internal network). Take a look at the Payment Card Industry Data Security Standards (PCI-DSS) : https://www.pcisecuritystandards.org/security_standards/pci_dss.shtml
There is a demo program for that specific reader that comes with VB source.
http://www.magtek.com/support/software/demo_programs/usb_swipe_insert.asp
Easiest way to download the Cab file from this link & include it in the project directory in a "magtek" folder.
http://www.magtek.com/support/software/demo_programs/card/usb_hid_swipe_readers/read_parse.asp
Add this code in aspx file after tag (change cab file src as per )
<object id="USBHID" classid="CLSID:22571E97-956A-4CDD-AF8D-AE9C26597683" codebase="magtek/99510060.CAB#version=1,13,0,2">
</object>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$('#txtNameFirst').focus(); // Focus on a textbox is required
USBHID.PortOpen = true;
if (USBHID.PortOpen == false) {
$('#<%= lblStatus.ClientID %>').text('Could not open MagTek reader');
}
else {
$('#<%= lblStatus.ClientID %>').text('Please Swipe a card');
}
});
$("#txtNameFirst").bind('change', function () {
var CCData = $("#txtNameFirst").val(); // CCData will contain the complete credit card data in a string.
alert(CCData);
$("#txtNameFirst").val(CCData.split('^')[1].split(' ')[0]);
$("#txtNameLast").val(CCData.split('^')[1].split(' ')[1]);
$("#txtCCNo").val(CCData.split('^')[0].substring(2, 18));
//alert(' Split1: ' + CCData.split('^')[1] + ' Split2: ' + CCData.split('^')[2]);
//alert('parsing good!');
$("#txtExpiDt_RoutingNo").val(CCData.split('^')[2].substring(2, 4) + '/' + CCData.split('^')[2].substring(0, 2));
});
</script>
As per the above code I have added focus on a text box . After swiping the card focused textboxes automatically show the complete credit card data string.

Multiple Flv Component playback - through a for loop - rewind issues AS3 Flash CS4

I am building a "video wall" app in flash AS3. I am importing a movie clip with a flvPlayback component nested within, then Adding it to the display list 12 times in a for loop (which is based on the length of an xml file.) The xml file aslo points to the .source of the flv instance.
This method is working, for displaying video content on all screens BUT, it only loops the last flvPlayback component. The rest just go back to the first frame of the video.
var vidURL = vidXML.video_item[i].#url
SS.video.source = vidURL;
SS.video.autoRewind = true;
SS.video.autoPlay = true;
SS.video.addEventListener(VideoEvent.COMPLETE, Loop);
function Loop(event:VideoEvent):void
{
SS.video.play();
}
I have tried refering to the SS + [i] to call the event to rewind as soon as it happens (as the videos are different lengths) but have had no luck.
Any help would be appreciated.
Thanks
Jono
Don't worry chaps...
using the "event.target.play()" is triggered when each video finishes, and rewinds them all nicely.
Sorry.
Jono