Xamarin android get screen size in px - xamarin

Hi i am trying to get the screen size of device
I have tried this code:
layout.Width;
layout.Height;
but the width and the height are returning 0,0
anybody knows how to get the screen size?

Xamarin "DetectScreenSize" Recipe:
var metrics = Resources.DisplayMetrics;
var widthInDp = ConvertPixelsToDp(metrics.WidthPixels);
var heightInDp = ConvertPixelsToDp(metrics.HeightPixels);
private int ConvertPixelsToDp(float pixelValue)
{
var dp = (int) ((pixelValue)/Resources.DisplayMetrics.Density);
return dp;
}
re: https://github.com/xamarin/recipes/blob/master/Recipes/android/resources/device_specific/detect_screen_size/DetectScreenSize/Activity1.cs

using Xamarin.Essentials
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
var realHeightInPx = mainDisplayInfo.Height;

Related

How to convert Xamarin Forms view to a Native view iOS/Android with the same size?

I am using the example here, but I am struggeling to fill in the right size parameter here so that it represents the same size as the forms view.
https://michaelridland.com/xamarin/creating-native-view-xamarin-forms-viewpage/
public static UIView ConvertFormsToNative(Xamarin.Forms.View view, CGRect size)
{
var renderer = RendererFactory.GetRenderer(view);
renderer.NativeView.Frame = size;
renderer.NativeView.AutoresizingMask = UIViewAutoresizing.All;
renderer.NativeView.ContentMode = UIViewContentMode.ScaleToFill;
renderer.Element.Layout(size.ToRectangle());
var nativeView = renderer.NativeView;
nativeView.SetNeedsLayout();
return nativeView;
}
And for Android:
public static ViewGroup ConvertFormsToNative(Xamarin.Forms.View view, Rectangle size)
{
var vRenderer = RendererFactory.GetRenderer (view);
var viewGroup = vRenderer.ViewGroup;
vRenderer.Tracker.UpdateLayout ();
var layoutParams = new ViewGroup.LayoutParams ((int)size.Width, (int)size.Height);
viewGroup.LayoutParameters = layoutParams;
view.Layout (size);
viewGroup.Layout (0, 0, (int)view.WidthRequest, (int)view.HeightRequest);
return viewGroup;
}
}
What values from my Xamarin Forms should be put in, so that it will represent the CGRect required to have the same on the Native View.
var rect = new CGRect();
rect.X = (nfloat)view.X;
rect.Y = (nfloat)view.Y;
rect.Width = (nfloat)view.Width;
rect.Height = (nfloat)view.Height;
Solved it, where "view" is the Xamarin Forms view.

ZXingBarcodeImageView (QR code) showing on iOS but not on Android (Xamarin.Forms)

This piece of code for showing a QR code in a Xamarin.Forms app works in iOS but not on Android:
let barCode = ZXingBarcodeImageView(HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
BarcodeFormat = ZXing.BarcodeFormat.QR_CODE,
BarcodeValue = foo)
barCode.BarcodeOptions.Width <- 500
barCode.BarcodeOptions.Height <- 500
mainLayout.Children.Add(barCode)
There's no error in the log, no exception thrown. Tried many heights and widths and different LayoutOptions to no avail. How can I debug this?
Luckily, I just had to play around with ZXing.Net.Mobile in my own Xamarin.Forms project. Where I managed to display the QRCode for both iOS and Android with the next C# code:
ZXingBarcodeImageView GenerateQR(string codeValue)
{
var qrCode = new ZXingBarcodeImageView
{
BarcodeFormat = BarcodeFormat.QR_CODE,
BarcodeOptions = new QrCodeEncodingOptions
{
Height = 350,
Width = 350
},
BarcodeValue = codeValue,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand
};
// Workaround for iOS
qrCode.WidthRequest = 350;
qrCode.HeightRequest = 350;
return qrCode;
}
Please note that there is a know issue in this library, and you have to set explicitly the WidthRequest & HeightRequest.
P.S.: More or less the same issue have been discussed here as well.

Google Maps Web (Nov 2017 New UI) Marker Icons

See the new 'flat' styled marker icons in Google Maps Web.
Anyone know where I can get a link to the image resources? I'm doing some custom Google Maps web work and want to use the new icons now.
Ok so found the answer by inspecting the new Google Maps web and using their assets:
var imageBlue = 'https://maps.gstatic.com/mapfiles/ms2/micons/blue-dot.png';
var imageRed = 'https://maps.gstatic.com/mapfiles/ms2/micons/red-dot.png';
var imageGreen = 'https://maps.gstatic.com/mapfiles/ms2/micons/green-dot.png';
var imageLtBlue = 'https://maps.gstatic.com/mapfiles/ms2/micons/ltblue-dot.png';
var imageYellow = 'https://maps.gstatic.com/mapfiles/ms2/micons/yellow-dot.png';
var imagePurple = 'https://maps.gstatic.com/mapfiles/ms2/micons/purple-dot.png';
var imagePink = 'https://maps.gstatic.com/mapfiles/ms2/micons/pink-dot.png';
// Custom images below taken from source images URLs of new Google Maps
// Can change HEX color values, scale=1 is small, scale=2 is bigger, scale=3 is biggest
var scale = 1;
var imageCustomPinSpotlight = 'https://www.google.com.au/maps/vt/icon/name=assets/icons/spotlight/spotlight_pin_v2_shadow-1-small.png,assets/icons/spotlight/spotlight_pin_v2-1-small.png,assets/icons/spotlight/spotlight_pin_v2_dot-1-small.png,assets/icons/spotlight/spotlight_pin_v2_accent-1-small.png&highlight=ff000000,ea4335,960a0a,ffffff&color=ff000000?scale=' + scale;
var imageCustomAirport = 'https://www.google.com.au/maps/vt/icon/name=assets/icons/poi/tactile/pinlet_shadow-1-small.png,assets/icons/poi/tactile/pinlet-1-small.png,assets/icons/poi/quantum/pinlet/airport_pinlet-1-small.png&highlight=ff000000,10bdff,ffffff&color=ff000000?scale=' + scale;
var imageCustomHospital = 'https://www.google.com.au/maps/vt/icon/name=assets/icons/poi/tactile/pinlet_shadow-1-small.png,assets/icons/poi/tactile/pinlet-1-small.png,assets/icons/poi/quantum/pinlet/hospital_H_pinlet-1-small.png&highlight=ff000000,f88181,ffffff&color=ff000000?scale=' + scale;
var imageCustomHospitalLite = 'https://www.google.com.au/maps/vt/icon/name=assets/icons/poi/tactile/pinlet_shadow-1-small.png,assets/icons/poi/tactile/pinlet-1-small.png,assets/icons/poi/quantum/pinlet/hospital_H_pinlet-1-small.png&highlight=ff000000,F5AFAF,ffffff&color=ff000000?scale=' + scale;
var imageCustomDotLtGreen = 'https://www.google.com.au/maps/vt/icon/name=assets/icons/poi/tactile/pinlet_shadow-1-small.png,assets/icons/poi/tactile/pinlet-1-small.png,assets/icons/poi/quantum/pinlet/dot_pinlet-1-small.png&highlight=ff000000,4db546,ffffff&color=ff000000?scale=' + scale;
var imageCustomDotRCBSRed = 'https://www.google.com.au/maps/vt/icon/name=assets/icons/poi/tactile/pinlet_shadow-1-small.png,assets/icons/poi/tactile/pinlet-1-small.png,assets/icons/poi/quantum/pinlet/dot_pinlet-1-small.png&highlight=ff000000,E42313,ffffff&color=ff000000?scale=' + scale;
var imageCustomStar = 'https://www.google.com.au/maps/vt/icon/name=assets/icons/poi/tactile/pinlet_shadow-1-small.png,assets/icons/poi/tactile/pinlet_outline-1-small.png,assets/icons/poi/tactile/pinlet-1-small.png,assets/icons/poi/quantum/pinlet/constellation_star_pinlet-1-small.png&highlight=ff000000,ffffff,fbc02d,ffffff&color=ff000000?scale=' + scale;
var imageCustomHeart = 'https://www.google.com.au/maps/vt/icon/name=assets/icons/poi/tactile/pinlet_shadow-1-small.png,assets/icons/poi/tactile/pinlet_outline-1-small.png,assets/icons/poi/tactile/pinlet-1-small.png,assets/icons/poi/quantum/pinlet/heart_pinlet-1-small.png&highlight=ff000000,ffffff,fa507d,ffffff&color=ff000000?scale=' + scale;
var imageCustomFlag = 'https://www.google.com.au/maps/vt/icon/name=assets/icons/poi/tactile/pinlet_shadow-1-small.png,assets/icons/poi/tactile/pinlet_outline-1-small.png,assets/icons/poi/tactile/pinlet-1-small.png,assets/icons/poi/quantum/pinlet/nickname_pinlet-1-small.png&highlight=ff000000,ffffff,4285f4,ffffff&color=ff000000?scale=' + scale;
var imageCustomHome = 'https://www.google.com.au/maps/vt/icon/name=assets/icons/poi/tactile/pinlet_outline-1-small.png,assets/icons/poi/tactile/pinlet-1-small.png,assets/icons/poi/quantum/pinlet/home_pinlet-1-small.png&highlight=ffffff,4285f4,ffffff&color=ff000000?scale=' + scale;

openseadragon get selection dataurl/blob

I retrieve a rect from openSeadragonSelection:
viewer:
this.viewer = OpenSeadragon(this.config);
this.selection = this.viewer.selection({
showConfirmDenyButtons: true,
styleConfirmDenyButtons: true,
returnPixelCoordinates: true,
onSelection: rect => console.log(rect)
});
this.selection.enable();
rect by onSelection:
t.SelectionRect {x: 3502, y: 2265, width: 1122, height: 887, rotation:0, degrees: 0, …}
I have no idea how to get the canvas by rect from my viewer instance.
this.viewer.open(new OpenSeadragon.ImageTileSource(this.getTile(this.src)));
A self implemented imageViewer returned the canvas of the selected area. So I could get the blob and post it to the server:
onSave(canvas){
let source = canvas.toDataURL();
this.setState({source:source, crop: false, angle: 0});
save(this.dataURItoBlob(source), source.match(new RegExp("\/(.*);"))1]);
}
dataURItoBlob(dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0)
byteString = atob(dataURI.split(',')[1]);
else
byteString = unescape(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to a typed array
var ia = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], {type:mimeString});
}
How can I get the image of the viewer by rect. Rotation should be considered as well.
#iangilman:
Thank's alot for your advice. I created another canvas which I crop and after that put it back into the viewer. I was not sure if something similar was supported by your library yet:
const viewportRect = self.viewer.viewport.imageToViewportRectangle(rect);
const webRect = self.viewer.viewport.viewportToViewerElementRectangle(viewportRect);
const { x, y, width, height } = webRect || {};
const { canvas } = self.viewer.drawer;
let source = canvas.toDataURL();
const img = new Image();
img.onload = function () {
let croppedCanvas = document.createElement('canvas');
let ctx = croppedCanvas.getContext('2d');
croppedCanvas.width = width;
croppedCanvas.height = height;
ctx.drawImage(img, x, y, width, height, 0, 0, width, height);
let croppedSrc = croppedCanvas.toDataURL();
//update viewer with cropped image
self.tile = self.getTile(croppedSrc);
self.ImageTileSource = new OpenSeadragon.ImageTileSource(self.tile);
self.viewer.open(self.ImageTileSource);
}
img.src = source;
Rotation hasn't been considered yet.
I imagine you'll need to convert the rectangle into the proper coordinates, then create a second canvas and copy the appropriate bit out of the OSD canvas into the second one.
Looks like maybe the selection rectangle is in image coordinates? The OSD canvas will be in web coordinates, or maybe double that on an HDPI display. OSD has a number of conversion functions, for instance:
var viewportRect = viewer.viewport.imageToViewportRectangle(imageRect);
var webRect = viewer.viewport.viewportToViewerElementRectangle(viewportRect);
You can find out the pixel density via OpenSeadragon.pixelDensityRatio.
Once you have the appropriate rectangle it should be easy to copy out of the one canvas into another. I'm not sure how you incorporate rotation, but it might be as simple as adding a rotation call to one of the canvas contexts.
Sorry this is kind of vague, but I hope it helps!

Kinetic JS - Change image resolution using filter when mouse is down

I am changing left mouse down (drag) to change the image, it works but very slow (refresh). I am using the following to display the image:
function makeKineticImage() {
dImage1 = new Kinetic.Image({
drawFunc: function(canvas) {
var context2 = canvas.getContext("2d");
var x = 0;
var y = 0;
context2.drawImage(dicom1, x, y);
imageData = context2.getImageData(x, y, dicom1.width, dicom1.height).data;
}
});
layer1.add(dImage1);
Then changing the image using Ajax:
...
}).done(function(d) {
dImage1.applyFilter(Kinetic.Filters.Grayscale, null, function() {
image.src = '/Home/changeImage?udm=' + (++udm);
layer1.draw();
});
I tried Grayscale filter, the refresh improved but not good enough. Is there a way to lower the resolution (down sampling). I would appreciate your suggestions, thanks in advance.

Resources