Read a Int64 in an ArrayBuffer using DataView - javascript

I am using Web Sockets.
I am passing an ArrayBuffer to my JavaScript page.
I have this in C# code:
byte[] packet = new byte[2];
packet[0] = (byte)1;
packet[1] = (byte)0;
byte[] tickArray = BitConverter.GetBytes( 635744635349556838 );
byte[] packet2 = new byte[2 + tickArray.Length];
Buffer.BlockCopy(packet, 0, packet2, 0, packet.Length);
Buffer.BlockCopy(tickArray, 0, packet2, packet.Length, tickArray.Length);
In my JavaScript Client I have this:
var dv = new DataView(e.data);
var marker = dv.getInt8(0);
var tripped = dv.getInt8(1);
var x = dv.getInt8(2);
I get the results:
1
0
-29
if change to this:
var x= dv.getInt16(1);
-7389
How do I get my value of 635744635349556838?
Thanks

I wrote a code. Would be it helpful?
function getUint64(bytes, littleEndian)
{
var low = 4, high = 0;
if (littleEndian)
{
low = 0;
high = 4;
}
var dv = new DataView(Uint8Array.from(bytes ).buffer);
return (dv.getUint32(high, littleEndian) << 32) |
dv.getUint32(low, littleEndian);
}
var bytes = [ 124, 22, 124, 22, 124, 22, 124, 22];
var value = getUint64(bytes, false);
console.log(value);

Related

How do I get last 32 bytes in Java

How do I get last 32 bytes in Java?
I tried this but the result is not same with javascript output.
byte[] privateKeyBytes32 = Arrays.copyOfRange(privateKeyBytes, privateKeyBytes.length - 32, privateKeyBytes.length);
byte[] publicKeyBytes32 = Arrays.copyOfRange(publicKeyBytes, publicKeyBytes.length - 32, publicKeyBytes.length);
Here is the code in reference code achieved inJavascript:
const computeSecretX25519 = (privKey, PubKey) => {
let privateKey = Buffer.from(privKey,'base64');
let publicKey = Buffer.from(PubKey, 'base64');
// real value is at last 32 bytes
const KEY_LENGTH = 32;
privateKey = privateKey.slice(privateKey.length - KEY_LENGTH);
publicKey = publicKey.slice(publicKey.length - KEY_LENGTH);
return sharedKey(privateKey, publicKey);
};

How can I align images using opencv.js

I am following this example from Satya Mallick
I hosted a test here https://icollect.money/opencv_align#
Problem: the findHomography() succeeds but the warpPerspective() fails with an 'unhandled exception'
I suspect that the homography is wrong as it looks like its an empty array:
h: Mat {$$: {…}}
cols: 0
data: Uint8Array(0)
data8S: Int8Array(0)
data16S: Int16Array(0)
data16U: Uint16Array(0)
data32F: Float32Array(0)
data64F: Float64Array(0)
matSize: Array(0)
rows: 0
I included the cpp code from the referenced article (above) inline with the javascript code:
function Align_img() {
//im2 is the original reference image we are trying to align to
let im2 = cv.imread(image_A_element);
//im1 is the image we are trying to line up correctly
let im1 = cv.imread(image_B_element);
//17 Convert images to grayscale
//18 Mat im1Gray, im2Gray;
//19 cvtColor(im1, im1Gray, CV_BGR2GRAY);
//20 cvtColor(im2, im2Gray, CV_BGR2GRAY);
let im1Gray = new cv.Mat();
let im2Gray = new cv.Mat();
cv.cvtColor(im1, im1Gray, cv.COLOR_BGRA2GRAY);
cv.cvtColor(im2, im2Gray, cv.COLOR_BGRA2GRAY);
//22 Variables to store keypoints and descriptors
//23 std::vector<KeyPoint> keypoints1, keypoints2;
//24 Mat descriptors1, descriptors2;
let keypoints1 = new cv.KeyPointVector();
let keypoints2 = new cv.KeyPointVector();
let descriptors1 = new cv.Mat();
let descriptors2 = new cv.Mat();
//26 Detect ORB features and compute descriptors.
//27 Ptr<Feature2D> orb = ORB::create(MAX_FEATURES);
//28 orb->detectAndCompute(im1Gray, Mat(), keypoints1, descriptors1);
//29 orb->detectAndCompute(im2Gray, Mat(), keypoints2, descriptors2);
var orb = new cv.ORB(5000);
orb.detectAndCompute(im1Gray, new cv.Mat(), keypoints1, descriptors1);
orb.detectAndCompute(im2Gray, new cv.Mat(), keypoints2, descriptors2);
//31 Match features.
//32 std::vector<DMatch> matches;
//33 Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-Hamming");
//34 matcher->match(descriptors1, descriptors2, matches, Mat());
let bf = new cv.BFMatcher(cv.NORM_HAMMING, true);
let matches = new cv.DMatchVector();
bf.match(descriptors1, descriptors2, matches);
//36 Sort matches by score
//37 std::sort(matches.begin(), matches.end());
//39 Remove not so good matches
//40 const int numGoodMatches = matches.size() * GOOD_MATCH_PERCENT;
//41 matches.erase(matches.begin()+numGoodMatches, matches.end());
let good_matches = new cv.DMatchVector();
for (let i = 0; i < matches.size(); i++) {
if (matches.get(i).distance < 30) {
good_matches.push_back(matches.get(i));
}
}
//44 Draw top matches
//45 Mat imMatches;
//46 drawMatches(im1, keypoints1, im2, keypoints2, matches, imMatches);
//47 imwrite("matches.jpg", imMatches);
let imMatches = new cv.Mat();
let color = new cv.Scalar(0,255,0, 255);
cv.drawMatches(im1, keypoints1, im2, keypoints2, good_matches, imMatches, color);
cv.imshow('imageCompareMatches', imMatches);
//50 Extract location of good matches
//51 std::vector<Point2f> points1, points2;
//53 for( size_t i = 0; i < matches.size(); i++ )
//54 {
//55 points1.push_back( keypoints1[ matches[i].queryIdx ].pt );
//56 points2.push_back( keypoints2[ matches[i].trainIdx ].pt );
//57 }
let points1 = [];
let points2 = [];
for (let i = 0; i < good_matches.size(); i++) {
points1.push(keypoints1.get(good_matches.get(i).queryIdx ).pt );
points2.push(keypoints2.get(good_matches.get(i).trainIdx ).pt );
}
//59 Find homography
//60 h = findHomography( points1, points2, RANSAC );
//The first 2 arguments to findHomography need to be matArray so you must convert your point1 and point2 to matArray
//reference: https://docs.opencv.org/3.4/d9/d0c/group__calib3d.html#ga4abc2ece9fab9398f2e560d53c8c9780
//*********** the issue seems to be here in how mat1 and mat2 are created *****
let mat1 = cv.matFromArray(points1.length, 2, cv.CV_32F, points1);
let mat2 = cv.matFromArray(points2.length, 2, cv.CV_32F, points2);
let h = cv.findHomography(mat1, mat2, cv.RANSAC);
//62 Use homography to warp image
//63 warpPerspective(im1, im1Reg, h, im2.size());
let image_B_final_result = new cv.Mat();
cv.warpPerspective(im1, image_B_final_result, h, im2.size());
cv.imshow('imageAligned', image_B_final_result);
matches.delete();
bf.delete();
orb.delete();
descriptors1.delete();
descriptors2.delete();
keypoints1.delete();
keypoints2.delete();
im1Gray.delete();
im2Gray.delete();
h.delete();
image_B_final_result.delete();
mat1.delete();
mat2.delete();
}
for (let i = 0; i < good_matches.size(); i++) {
points1.push(keypoints1.get(good_matches.get(i).queryIdx).pt);
points2.push(keypoints2.get(good_matches.get(i).trainIdx).pt);
}
shoud be below code
for (let i = 0; i < good_matches.size(); i++) {
points1.push(keypoints1.get(good_matches.get(i).queryIdx).pt.x);
points1.push(keypoints1.get(good_matches.get(i).queryIdx).pt.y);
points2.push(keypoints2.get(good_matches.get(i).trainIdx).pt.x);
points2.push(keypoints2.get(good_matches.get(i).trainIdx).pt.y);
}
I push staggered data into points[], then it's work!

ArrayBuffer and Float64Array in Swift

I'm trying to convert one of the javascript functions into Swift 5 function. But even after so much of extensive search I couldn't find anything on it.
function toArrayBuffer(type, ts, x, y, z, sc) {
let userId = userID.getUserId();
//console.log("Found GPS UserID: " + userId);
if (userId == "Unauthor") {
//console.log("GPS did not find the userID")
return null;
}
let buffer = new ArrayBuffer(8 * 4);
// Offset: 0
let float64Bytes = new Float64Array(buffer);
float64Bytes[0] = ts;
// Offset: 8
let bytes = new Float32Array(buffer);
bytes[2] = x;
bytes[3] = y;
bytes[4] = z;
bytes[5] = sc;
// Offset: 24
let uint8Bytes = new Uint8Array(buffer);
uint8Bytes[24] = type;
for (let i = 0; i < 8; i++) {
if (userId.charCodeAt(i)) {
uint8Bytes[i + 25] = userId.charCodeAt(i);
}
}
return buffer;
}
Basically I tried to build the same function with var byteArray = [UInt8](stringVal.utf8) but UInt8 can store only upto 256, but I had to store epoch time stamp. So, it doesn't work too. Any help would be appreciated.

trying to put in a command that will divide #'s divisible by 3,5, & 15

Trying to get the code to divide by 3,5,& 15. Keep getting syntax error.
//dividing by three
var pingpong = function(number) {
var threenumbers = ['3,6,9,12'];
var bythree = [];
threenumbers.forEach(function(number) {
bythree.push(number / 3);
{
var fivenumbers = ['5,10,15,20'];
var byfive = [];
fivenumbers.forEach(function(number){
byfive.push(number / 5);
var fifteennumbers = ['15, 25, 30, 36'];
var byfifteen = [];
fifteennumbers.forEach(function(number) {
byfifteen.push( number / 15);
};
how do I get it to run the code and divide it by the numbers?
uncaught syntax error?
I don't understand what you want to happen but ...
1- the data in the arrays threenumbers fivenumbers and fifteennumbers are strings not number.
2- as said in 1 the arrays have one string each so using forEach(function(number){ will iterate one time setting number to "3,6,9,12" "5,10,15,20" "15, 25, 30, 36".
3- you had a bunch of parenthesis open but not closed.
var pingpong = function(number) {
var threenumbers = [3,6,9,12];
var bythree = [];
threenumbers.forEach(function(number) {
bythree.push(number / 3);
var fivenumbers = [5,10,15,20];
var byfive = [];
fivenumbers.forEach(function(number){
byfive.push(number / 5);
var fifteennumbers = [15, 25, 30, 36];
var byfifteen = [];
fifteennumbers.forEach(function(number) {
byfifteen.push( number / 15);
});
});
});
}
pingpong(40);
this doesn't yell errors at you but also probably doesn't do what you want as I said I really dont know what you want

How to make soundfile in javascript?

i wanted to make a code which generates a sound file in wav format. However, i got stuck whet trying to play it. This is what i have done so far:
var fs = require("fs");
var buf = new Buffer(176400);
var fileName = "i_write_this_wave.wav";
var fd = fs.openSync(fileName, "w");
buf.writeUInt32BE(0x52494646, 0);
buf.writeUInt32LE(0x24080000, 4);
buf.writeUInt32BE(0x57415645, 8);
buf.writeUInt32BE(0x666d7420, 12);
buf.writeUInt32LE(0x10000000, 16);
buf.writeUInt16LE(0x0100, 20);
buf.writeUInt16LE(0x0200, 22);
buf.writeUInt32LE(0x22560000, 24);
buf.writeUInt32LE(0x88580100, 28);
buf.writeUInt16LE(0x0400, 32);
buf.writeUInt16LE(0x1000, 34);
buf.writeUInt32BE(0x64617461, 36);
buf.writeUInt32LE(0x00080000, 40);
var vl = 32000;
var of = 44;
while (of < 176400) {
buf.writeUInt16LE(vl, of);
of = of + 2;
}
fs.writeSync(fd, buf, 0, buf.length);
It makes the wav file with right header, etc, but i am not able to play it. I think there is a problem with the buffer size, but what should be the right size of the buffer? If you have any suggestions please here with them.
You are misusing writeUInt16LE and writeUInt32LE. If you want to write the 32-bit integer 0x5622 in little endian, for instance, you should use
buf.writeUInt32LE(0x5622, 24);
You are reversing every value that you write in little endian, resulting in those appearing in the file in big endian, which is going to result in an invalid file.
For concreteness, here is how I would rewrite your code:
var fs = require("fs");
var buf = new Buffer(176400);
var fileName = "i_write_this_wave.wav";
var fd = fs.openSync(fileName, "w");
buf.writeUInt32BE(0x52494646, 0);
buf.writeUInt32LE(buf.length-8, 4);
buf.writeUInt32BE(0x57415645, 8);
buf.writeUInt32BE(0x666d7420, 12);
buf.writeUInt32LE(0x10, 16);
buf.writeUInt16LE(0x01, 20);
buf.writeUInt16LE(0x02, 22);
buf.writeUInt32LE(0x5622, 24);
buf.writeUInt32LE(0x15888, 28);
buf.writeUInt16LE(0x04, 32);
buf.writeUInt16LE(0x10, 34);
buf.writeUInt32BE(0x64617461, 36);
buf.writeUInt32LE(buf.length-44, 40);
var vl = 32000;
var of = 44;
while (of < 176400) {
buf.writeUInt16LE(vl, of);
of = of + 2;
}
fs.writeSync(fd, buf, 0, buf.length);

Categories

Resources