Reputation: 1686
I dont have any idea how to generate UUID of a certain image or video. I want to generate a UUID of image or video in able to send it to web api. Can anyone know how to do this? Thanks in advance.
Upvotes: 0
Views: 4961
Reputation: 954
EDIT - there's a java.util.UUID.nameUUIDFromBytes( )
function for just this purpose! All this code below is unnecessary
This is 5 years after the question, but a use case came up where it's not totally irrational to generate a UUID "from" an image. The scenario is a customer is uploading an image to a PaaS, the internals of which demand UUIDs as part of every request so that POSTs/PUTs can be idempotent. The PaaS is all message-driven architecture so lots of queues and retries, etc. The customer doesn't have much control over the upload and all they can do at the moment is POST a binary image.
So the idea is to "extract" a UUID from an image as it enters the PaaS and use that UUID to identify the image (e.g. "we've seen this before! no need to continue processing"). Why not just hash the image and use that hash? Because the PaaS's internal APIs are all UUID based and this is the quickest, albeit dirtiest approach.
This is a mock-up of a test+code in Java that can take a PNG or JPG and deterministically "fill in" the bytes of a UUID based on the contents of the image:
package com.mycompany;
import static com.google.common.base.Preconditions.checkState;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.UUID;
import javax.imageio.ImageIO;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class TestUuidFromImage {
// These must be sorted in ASCII byte-order
private static final byte[] VALID_UUID_BYTES = "0123456789ABCDEFabcdef".getBytes();
private static final byte[] VALID_UUID_16TH_BYTES = "89AB".getBytes();
@Test
public void testJpgUuid() throws IOException {
byte[] imageBytes = loadImageBytes("cat.jpg", "jpg");
assertUuid(imageBytes, UUID.fromString("ffc70144-4982-42c2-aa2b-3b456789cdef"));
}
@Test
public void testPngUuid() throws IOException {
byte[] imageBytes = loadImageBytes("dog.png", "png");
assertUuid(imageBytes, UUID.fromString("d9daccee-39dd-4c4d-855d-9376bc981c11"));
}
void assertUuid(byte[] imageBytes, UUID expectedUuid) throws IOException {
assertEquals(expectedUuid, createUuidFromBytes(imageBytes));
}
byte[] loadImageBytes(String resourcePath, String formatName) throws IOException {
BufferedImage img = ImageIO.read(getClass().getResource(resourcePath));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, formatName, baos );
baos.flush();
return baos.toByteArray();
}
/*
This loops from 0...N over the byte[] of an image, trying to "fill"
a 32-byte array with valid UUID characters. If it can do so, the
32-byte array of valid UUID characters is returned as a java.util.UUID
*/
private UUID createUuidFromBytes(byte[] bytes) {
byte[] uuidBytes = new byte[32];
int uuidByte = 0;
for (int j=0; j<bytes.length; j++) {
if (isUuidByte(uuidByte, bytes[j])) {
uuidBytes[uuidByte] = bytes[j];
uuidByte++;
}
if (uuidByte == 32) {
break;
}
}
checkState(uuidByte == 32, "Couldn't find 32 good UUID-valid bytes in the whole image!");
String uuidString = String.format("%s-%s-%s-%s-%s",
new String(Arrays.copyOfRange(uuidBytes, 0, 8)),
new String(Arrays.copyOfRange(uuidBytes, 8, 12)),
new String(Arrays.copyOfRange(uuidBytes, 12, 16)),
new String(Arrays.copyOfRange(uuidBytes, 16, 20)),
new String(Arrays.copyOfRange(uuidBytes, 20, 32))).toUpperCase();
return UUID.fromString(uuidString);
}
private boolean isUuidByte(int index, byte imageByte) {
switch (index) {
case 12:
// The leading byte of the 3rd section in UUID4s is always "4"
return '4' == imageByte;
case 16:
// The leading byte of the 4th section in UUID4s is one of "8", "9", "A" or "B"
return Arrays.binarySearch(VALID_UUID_16TH_BYTES, imageByte) >= 0;
default:
return Arrays.binarySearch(VALID_UUID_BYTES, imageByte) >= 0;
}
}
}
I can't vouch for how collision-prone this is in practice, but if you have a wide variety of image data sources it might be useable. -- Hope this helps someone.
Upvotes: 0
Reputation: 12416
You're doing something wrong if you want to generate UUID based on image/video. UUID stands for "universally unique identifier". Read this as random, but unique identifier (number).
UPDATE2
To generate new UUID (without notion of image/video) you can do:
java.util.UUID.randomUUID()
Upvotes: 2