Displaying image from device in ionic - ionic-framework

Let's say I have an image under file:///storage/emulated/0/Android/data/io.ionic.starter/cache/1542035689920.jpg path. Is it possible to display this image in <image> tag just by assigning .url property? Probably not, if so are there any good solutions to do it? I'm trying to do a thumbnail of an image in the list, the way I've done it is to read an image as base64, convert it with the usage of canvas then display it. Problem is it takes too much time to render all the images.
ionViewDidLoad() {
this.defectImages.forEach(defectImage => this.loadThumbnail(defectImage.url))
}
loadThumbnail(url) {
return this.fileProvider.getDefectCachedImage(url)
.then(data => {
return this.generateFromImage(data, 400, 400, 0.5, thumbnail => {
let image = document.getElementById(url) as any;
image.src = thumbnail;
})
});
}
showImage(url: string) {
this.photoViewer.show(this.fileProvider.getCacheFolderPath() + url, '', {share: true})
}
generateFromImage(img, MAX_WIDTH: number = 700, MAX_HEIGHT: number = 700, quality: number = 0.5, callback) {
var canvas: any = document.createElement("canvas");
var image = new Image();
image.onload = () => {
var width = image.width;
var height = image.height;
if (width > height) {
if (width > MAX_WIDTH) {
height *= MAX_WIDTH / width;
width = MAX_WIDTH;
}
} else {
if (height > MAX_HEIGHT) {
width *= MAX_HEIGHT / height;
height = MAX_HEIGHT;
}
}
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext("2d");
ctx.drawImage(image, 0, 0, width, height);
// IMPORTANT: 'jpeg' NOT 'jpg'
var dataUrl = canvas.toDataURL('image/jpeg', quality);
callback(dataUrl)
};
image.src = img;
}
It is working but working too slow. I have noticed PhotoLibrary package but as far I know it access to the shared memory and my pictures are being held in app only space.

This is the issue with the latest webview. The latest webview is not allowing the local resources to load. It was working fine on Ionic 2. Though not an optimal solution, I'm pasting the solution that worked for me.
ionic cordova plugins rm cordova-plugin-ionic-webview
ionic cordova plugins add cordova-plugin-ionic-webview#1.2.1
cordova clean android
And you can build(ionic cordova build android) the app and test it.

Related

How to apply different frames on image border in flutter

I search a lot how to apply frame on images but found no answer. I read stack function in which we can put an image inside other but its not good trick. I think there must be library for frame and by using that frame can automatically apply on border of image. Please guide me how can I use different design of frame outside image like some filters application.
I want to give options of frame to user like below and when user click on any frame that will apply on image
There are many approaches for that issue. I've used several in the past. It strongly differs in the way you want to make use of the image. Do you need to paint something on the image or just display it?
In my opinion multiple frames on an image can be achieved best with CustomPainter. Check out the documentation for more. The following Painter should do the trick.
class TestPainter extends CustomPainter {
final ui.Image image;
TestPainter({required this.image});
#override
void paint(Canvas canvas, Size size) {
//draw the image
canvas.drawImage(image, const Offset(0, 0), Paint());
double rectWidth = image.width.toDouble();
double rectHeight = image.height.toDouble();
final imageCenter = Offset(image.width / 2, image.height / 2);
//first frame
var border1 = Rect.fromCenter(
center: imageCenter,
width: rectWidth + 1,
height: rectHeight + 1);
var painting = Paint();
painting.color = Colors.red;
painting.strokeWidth = 2;
painting.style = PaintingStyle.stroke;
canvas.drawRect(border1, painting);
//second frame
var border2 = Rect.fromCenter(
center: imageCenter,
width: rectWidth + 3,
height: rectHeight + 3);
var painting2 = Paint();
painting2.color = Colors.green;
painting2.strokeWidth = 2;
painting2.style = PaintingStyle.stroke;
canvas.drawRect(
border2,
painting2,
);
}
#override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true;
}
}
Please be cautious. The Image in that example needs to be an Image of the dart:ui library (Documentation). There are various way to convert. Depends on your project.

Unity Texture2D.ReadPixels reads differently on Android

We're trying to capture an area of the screen for our players to share. In the editor, the capture works fine, it captures the exact area. On Android however, the area seems to vary on the device itself. On one device it captures a much larger area and on another it's a smaller area. But, the final image size is always the same. This makes me think that a scaling or translation isn't correct when capturing the screen. However, visually everything scales correctly.
I'm using Unity 2020.1.10
I assign the panel I want a screenshot of to recT.
public class ElementScreenshot : MonoBehaviour
{
public RectTransform rectT; // Assign the UI element which you wanna capture
//public Image img;
int width; // width of the object to capture
int height; // height of the object to capture
// Use this for initialization
void Start()
{
width = System.Convert.ToInt32(rectT.rect.width);
height = System.Convert.ToInt32(rectT.rect.height);
}
public IEnumerator takeScreenShot(string filePath)
{
yield return new WaitForEndOfFrame(); // it must be a coroutine
Vector2 temp = rectT.transform.position;
var startX = temp.x - width / 2;
var startY = temp.y - height / 2;
var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(startX, startY, width, height), 0, 0);
tex.Apply();
// Encode texture into PNG
var bytes = tex.EncodeToPNG();
Destroy(tex);
File.WriteAllBytes(filePath, bytes);
}
public string Capture()
{
var filePath = Path.Combine(Application.temporaryCachePath, "shared_image.png");
StartCoroutine(takeScreenShot(filePath)); // screenshot of a particular UI Element.
return filePath;
}
}
The issue was with using rect. The following change fixed the issue.
public IEnumerator takeScreenShot(string filePath)
{
yield return new WaitForEndOfFrame(); // it must be a coroutine
var r = RectTransformToScreenSpace(rectT);
var tex = new Texture2D((int)r.width, (int)r.height, TextureFormat.RGB24, false);
tex.ReadPixels(r, 0, 0);
tex.Apply();
// Encode texture into PNG
var bytes = tex.EncodeToPNG();
Destroy(tex);
File.WriteAllBytes(filePath, bytes);
}
public static Rect RectTransformToScreenSpace(RectTransform transform)
{
Vector2 size = Vector2.Scale(transform.rect.size, transform.lossyScale);
return new Rect((Vector2)transform.position - (size * 0.5f), size);
}

Take picture from Gallery And Crop that picture in the selected circle portion In Unity 3d?

I am working on a project. I want to take a picture from gallery of android device.
1. How can i take a picture from gallery of android device?
2. How can i Crop the picture selected circle portion in unity3d?
You can use below code for cropping image. I only pass Texture2D and set centre and radius automatically according to texture; but you can pass them manually if you want to.
Texture2D RoundCrop (Texture2D sourceTexture) {
int width = sourceTexture.width;
int height = sourceTexture.height;
float radius = (width < height) ? (width/2f) : (height/2f);
float centerX = width/2f;
float centerY = height/2f;
Vector2 centerVector = new Vector2(centerX, centerY);
// pixels are laid out left to right, bottom to top (i.e. row after row)
Color[] colorArray = sourceTexture.GetPixels(0, 0, width, height);
Color[] croppedColorArray = new Color[width*height];
for (int row = 0; row < height; row++) {
for (int column = 0; column < width; column++) {
int colorIndex = (row * width) + column;
float pointDistance = Vector2.Distance(new Vector2(column, row), centerVector);
if (pointDistance < radius) {
croppedColorArray[colorIndex] = colorArray[colorIndex];
}
else {
croppedColorArray[colorIndex] = Color.clear;
}
}
}
Texture2D croppedTexture = new Texture2D(width, height);
croppedTexture.SetPixels(croppedColorArray);
croppedTexture.Apply();
return croppedTexture;
}

How display a resized image with aspect ratio preserved in a fixed size Label in eclipse e4 rcp?

I want to display a preview image which is a resized version of the original image with aspect ratio preserved in a fixed size Label. For example, I have an image of 1024x786 and I want to display it in a Label which has size 500x500. I want it to fit in this label keeping the aspect ratio of the image intact.
It would also be nice if the image could be automatically resized when a user resizes the part.
Is it possible to do this with Label? or do I need a canvas for this?
This image scaling code is based on JFace ImageDescriptor:
ImageDescriptor scaleImage(Display display, ImageDescriptor imageDesc,
int maxWidth, int maxHeight)
{
if (imageDesc == null)
return null;
ImageData imageData = imageDesc.getImageData();
if (imageData == null) // Can by JPEG using CMYK colour space etc.
return imageDesc;
int newHeight = maxHeight;
int newWidth = (imageData.width * newHeight) / imageData.height;
if (newWidth > maxWidth)
{
newWidth = maxWidth;
newHeight = (imageData.height * newWidth) / imageData.width;
}
// Use GC.drawImage to scale which gives better result on Mac
Image newImage = new Image(display, newWidth, newHeight);
GC gc = new GC(newImage);
Image oldImage = imageDesc.createImage();
gc.drawImage(oldImage, 0, 0, imageData.width, imageData.height, 0, 0, newWidth, newHeight);
ImageDescriptor result = ImageDescriptor.createFromImage(newImage);
oldImage.dispose();
gc.dispose();
return result;
}

Unity 4.6 image scaled disappeared

I need to resize an image to be sure it is a square.
So I do
var rectTransform = GetComponent<RectTransform>();
var width = rectTransform.rect.width;
var height = rectTransform.rect.height;
if (Math.Abs(width - height) > 0.1f)
{
float size = width < height? width : height;
transform.localScale = new Vector3(size/width,size/height);
}
When I play it I see that the rectTransform has the good size but the sprite as disappear an the image is now transparent, does somebody know about it?
Using unity V4.6b20
Problem solved by doing
var rectTransform = GetComponent<RectTransform>();
var width = rectTransform.sizeDelta.x;
var height = rectTransform.sizeDelta.y;
if (Math.Abs(width - height) > 0.1f)
{
float size = width < height ? width : height;
rectTransform.sizeDelta=new Vector2(size,size);
}