I captured an image from createcapture() through a webcam. Using image(), I got the image onto the canvas. Using saveframes(), and a callback, I was able to send the image to a server, but the image size is very big and I want the image to be 100 x 100 pixels every time whatever may be the size of image from saveframes() before sending it to server.
var x = window.innerWidth * 0.5;
var y = window.innerHeight * 0.75;
function setup()
{
cnv = createCanvas(x, y);
videoInput = createCapture(VIDEO);
videoInput.size(x, y);
videoInput.hide();
}
function draw()
{
if (videoInput) {
image(videoInput, 0, 0, x, y);
}
}
function image()
{
saveFrames('out','png',1,1,
function(im) {
console.log(im);
console.log(typeof im[0]);
// image Data is stored in im[0].imageData
// convert the image to smaller size
data.imageData = im[0].imageData;
// data.imageData = Math.random();
}
);
$.post('addname',data,function(result) {
console.log("data sent", result);
});
}
Related
I have a drawImage function in my application which loads the image from a blob and then from that image object calls the toDataURL function for the canvas to draw image. The problem is that this code works on android and browser but not working on iphone or ipad.
I have tried passing the image type to the toDataURL function and tried setting the default width and height of the canvas to prevent width getting 0 for the canvas but nothing seems to work on iphone or ipad. It only shows a blank screen inplace of the image.
the code works fine on mac/safari as well but not in iphone/ipad on any browser.
here is my drawImage function
_drawImage(img) {
// console.log("drawimage");
this.setState({ isGenerating: true });
if (img) {
fetch(img)
.then((r) => r.blob())
.then((blob) => {
loadImage(
blob,
(img) => {
if (img.type === "error") {
this.setState({ error: true });
// console.log(img, "Error loading image ");
} else {
const image = new window.Image();
const height = this.props.height;
const width = this.props.width;
image.src = img.toDataURL('image/jpeg');
console.log(image.src, height, width)
image.onload = () => {
let offsetX,
offsetY,
renderableWidth = image.width,
renderableHeight = image.height;
var imageDimensionRatio = image.width / image.height;
var canvasDimensionRatio = width / height;
// console.log(
// "ratios of image and canvas =",
// imageDimensionRatio,
// canvasDimensionRatio
// );
if (imageDimensionRatio < canvasDimensionRatio) {
renderableHeight = height;
renderableWidth =
image.width * (renderableHeight / image.height);
offsetX = (width - renderableWidth) / 2;
offsetY = 0;
} else if (imageDimensionRatio > canvasDimensionRatio) {
renderableWidth = width;
renderableHeight =
image.height * (renderableWidth / image.width);
offsetX = 0;
offsetY = (height - renderableHeight) / 2;
} else {
renderableHeight = height;
renderableWidth = width;
offsetX = 0;
offsetY = 0;
}
console.log(renderableHeight, renderableWidth, image)
this.setState(
{
image: image,
renderableHeight,
renderableWidth,
imgHeight: image.height,
imgWidth: image.width,
offsetX,
offsetY,
height,
width,
},
() => {
this.setState({ isGenerating: false });
}
);
};
}
},
{
orientation: true,
canvas: true,
}
);
});
}
}
EDIT
I've solved the issue by passing maxWidth to loadImage params along with orientation and canvas, which limits the width to not to exceed in ios devices and thus onload function works properly.
basically I've added the below code.
maxWidth: 10000
Currently am implementing zoomin and zoomout using twofingers zoomin and zoomout works as expected but we are showing percentage values in while zoomin and zoomout if possible to show the values using Textview or anyother way to show the zooming percentage values in view in android xamarin
public override bool OnTouchEvent(MotionEvent e)
{
switch (e.Action & MotionEventActions.Mask)
{
case MotionEventActions.Down:
oldDist = getFingerSpacing(e);
break;
case MotionEventActions.Move:
float newDist = getFingerSpacing(e);
if (newDist > oldDist)
{
//mCamera is your Camera which used to take picture, it should already exit in your custom Camera
handleZoom(true, camera);
}
else if (newDist < oldDist)
{
handleZoom(false, camera);
}
oldDist = newDist;
break;
}
return true;
}
private void handleZoom(bool isZoomIn, global::Android.Hardware.Camera camera)
{
global::Android.Hardware.Camera.Parameters parameters = camera.GetParameters();
var s = camera.GetParameters().ZoomRatios;
if (parameters.IsZoomSupported)
{
int maxZoom = parameters.MaxZoom;
int zoom = parameters.Zoom;
if (isZoomIn && zoom < maxZoom)
{
zoom++;
}
else if(zoom > 0)
{
zoom--;
}
parameters.Zoom = zoom;
var zoomValue = Convert.ToDecimal(s[zoom]);
zoomValue = Math.Round((zoomValue / 100), 2);
Toast toast = Toast.MakeText(Android.App.Application.Context, _languageCache.Translate(zoomValue + "%"), ToastLength.Short);
toast.SetGravity(GravityFlags.Center, 0, 0);
toast.Show();
camera.SetParameters(parameters);
PrepareAndStartCamera();
}
else
{
Android.Util.Log.Error("lv", "zoom not supported");
}
}
private static float getFingerSpacing(MotionEvent e)
{
if(e.PointerCount==2)
{
int pointerIndex = e.FindPointerIndex(_activePointerId);
float x = e.GetX(pointerIndex);
float y = e.GetY(pointerIndex);
return (float)Math.Sqrt(x * x + y * y);
}
}
You could use the ZoomRatios to get the zoom ratios of all zoom values. Use the zoom ratios in 1/100 increments. The list is sorted from small to large. The first element is always 100. The last element is the zoom ratio of the maximum zoom value.
You could call this after IsZoomSupported in handleZoom method.
var s = camera.GetParameters().ZoomRatios;
var zoomValue = Convert.ToDecimal(s[zoom]);
value = Math.Round((zoomValue / 100), 2);
You could set the percentage like value + "x":
2.5x // a zoom of 2.5x is returned as 250.
After that you could show this value with a dialpg alert or custom the camera layout as your own.
Update:
Add a TextView to show the zoom value in the screen like below.
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:elevation="10dp"
android:id="#+id/textView1" />
And then set the value in your OnTouchEvent.
if (value != 0)
{
textView.Text = value+"x";
}
I'm trying detect if a pixel is complete transparent or not. More specifically, if an image is on a transparent background or not. I thought that
// Add a Color Sampler at a given x and y coordinate in the image.
var pointSample = app.activeDocument.colorSamplers.add([(x),(y)]);
// Obtain array of RGB values.
var rgb = [
pointSample.color.rgb.red,
pointSample.color.rgb.green,
pointSample.color.rgb.blue
];
would work - and it does...
Unless the pixel has 0% opacity (ie 100% transparent) in which case Photoshop throws a General 8800 error. I would use the above with a try catch but that probably isn't the way to go about it.
Any ideas?
Well I found a way to determine if an image has transparency or not:
delete_all_colour_samples();
var isImageTransparent = has_transparency();
var x = 0;
var y = 0;
alert(isImageTransparent ? "Image has transparency." : "Image is opaque.");
if (isImageTransparent)
{
// do stuff
}
else
{
// get colour sample
// Add a Color Sampler at a given x and y coordinate in the image.
var pointSample = app.activeDocument.colorSamplers.add([(x),(y)]);
// Obtain array of RGB values.
var rgb = [
pointSample.color.rgb.red,
pointSample.color.rgb.green,
pointSample.color.rgb.blue
];
}
// function to see if image has transparency
function has_transparency()
{
var desc52 = new ActionDescriptor();
var ref47 = new ActionReference();
ref47.putProperty( charIDToTypeID('Chnl'), charIDToTypeID('fsel') );
desc52.putReference( charIDToTypeID('null'), ref47 );
var ref48 = new ActionReference();
ref48.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Trsp') );
desc52.putReference( charIDToTypeID('T '), ref48 );
desc52.putBoolean( charIDToTypeID('Invr'), true );
try
{
executeAction( charIDToTypeID('setd'), desc52, DialogModes.NO );
}
catch(eek)
{
return false;
}
activeDocument.selection.deselect();
return true;
}
function delete_all_colour_samples()
{
// Kill all colour samples
app.activeDocument.colorSamplers.removeAll();
}
I am using BackgroundSubtractorMOG2() to extract foreground of video.I am wondering about "how to get original colors of the image and set it foreground".I want to extract objects with original colors not threshold.
Foreground Extraction Code:
let video = document.getElementById('videoInput');
let cap = new cv.VideoCapture(video);
let frame = new cv.Mat(video.height, video.width, cv.CV_8UC4);
let fgmask = new cv.Mat(video.height, video.width, cv.CV_8UC1);
let fgbg = new cv.BackgroundSubtractorMOG2(500, 16, true);
const FPS = 30;
function processVideo() {
try {
if (!streaming) {
// clean and stop.
frame.delete(); fgmask.delete(); fgbg.delete();
return;
}
let begin = Date.now();
// start processing.
cap.read(frame);
fgbg.apply(frame, fgmask);
cv.imshow('canvasOutput', fgmask);
// schedule the next one.
let delay = 1000/FPS - (Date.now() - begin);
setTimeout(processVideo, delay);
} catch (err) {
utils.printError(err);
}
};
// schedule the first one.
setTimeout(processVideo, 0);
Original image
My output
I want this
I'm trying just to run the same procedure, but with different scale. But it gives me broken pictures. So what is the correct way to update scale?
function draw(num, scale) {
pdfDoc.getPage(num).then(function(page) {
var viewport = page.getViewport(scale);
var canvas = document.getElementById('the-canvas');
var context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
var renderContext = {
canvasContext: context,
viewport: viewport
};
page.render(renderContext);
}
}
draw(1, 1);
draw(1, 2);