| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.hardware.camera2; |
| 18 |
|
| 19 |
import android.annotation.IntRange; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.graphics.Bitmap; |
| 22 |
import android.graphics.Color; |
| 23 |
import android.graphics.ImageFormat; |
| 24 |
import android.hardware.camera2.impl.CameraMetadataNative; |
| 25 |
import android.location.Location; |
| 26 |
import android.media.ExifInterface; |
| 27 |
import android.media.Image; |
| 28 |
import android.os.SystemClock; |
| 29 |
import android.util.Log; |
| 30 |
import android.util.Size; |
| 31 |
|
| 32 |
import java.io.IOException; |
| 33 |
import java.io.InputStream; |
| 34 |
import java.io.OutputStream; |
| 35 |
import java.nio.ByteBuffer; |
| 36 |
import java.text.DateFormat; |
| 37 |
import java.text.SimpleDateFormat; |
| 38 |
import java.util.Calendar; |
| 39 |
import java.util.Locale; |
| 40 |
import java.util.TimeZone; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public final class DngCreator implements AutoCloseable { |
| 67 |
|
| 68 |
private static final String TAG = "DngCreator"; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public DngCreator(@NonNull CameraCharacteristics characteristics, |
| 88 |
@NonNull CaptureResult metadata) { |
| 89 |
if (characteristics == null || metadata == null) { |
| 90 |
throw new IllegalArgumentException("Null argument to DngCreator constructor"); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
long currentTime = System.currentTimeMillis(); |
| 95 |
|
| 96 |
long timeOffset = 0; |
| 97 |
|
| 98 |
int timestampSource = characteristics.get( |
| 99 |
CameraCharacteristics.SENSOR_INFO_TIMESTAMP_SOURCE); |
| 100 |
|
| 101 |
if (timestampSource == CameraCharacteristics.SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) { |
| 102 |
|
| 103 |
|
| 104 |
timeOffset = currentTime - SystemClock.elapsedRealtime(); |
| 105 |
} else if (timestampSource == CameraCharacteristics.SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN) { |
| 106 |
|
| 107 |
|
| 108 |
timeOffset = currentTime - SystemClock.uptimeMillis(); |
| 109 |
} else { |
| 110 |
|
| 111 |
Log.w(TAG, "Sensor timestamp source is unexpected: " + timestampSource); |
| 112 |
timeOffset = currentTime - SystemClock.uptimeMillis(); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
Long timestamp = metadata.get(CaptureResult.SENSOR_TIMESTAMP); |
| 117 |
long captureTime = currentTime; |
| 118 |
if (timestamp != null) { |
| 119 |
captureTime = timestamp / 1000000 + timeOffset; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
final DateFormat dateTimeStampFormat = |
| 125 |
new SimpleDateFormat(TIFF_DATETIME_FORMAT, Locale.US); |
| 126 |
dateTimeStampFormat.setTimeZone(TimeZone.getDefault()); |
| 127 |
|
| 128 |
|
| 129 |
String formattedCaptureTime = dateTimeStampFormat.format(captureTime); |
| 130 |
|
| 131 |
nativeInit(characteristics.getNativeCopy(), metadata.getNativeCopy(), |
| 132 |
formattedCaptureTime); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
@NonNull |
| 157 |
public DngCreator setOrientation(int orientation) { |
| 158 |
if (orientation < ExifInterface.ORIENTATION_UNDEFINED || |
| 159 |
orientation > ExifInterface.ORIENTATION_ROTATE_270) { |
| 160 |
throw new IllegalArgumentException("Orientation " + orientation + |
| 161 |
" is not a valid EXIF orientation value"); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
if (orientation == ExifInterface.ORIENTATION_UNDEFINED) { |
| 166 |
orientation = TAG_ORIENTATION_UNKNOWN; |
| 167 |
} |
| 168 |
nativeSetOrientation(orientation); |
| 169 |
return this; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
@NonNull |
| 187 |
public DngCreator setThumbnail(@NonNull Bitmap pixels) { |
| 188 |
if (pixels == null) { |
| 189 |
throw new IllegalArgumentException("Null argument to setThumbnail"); |
| 190 |
} |
| 191 |
|
| 192 |
int width = pixels.getWidth(); |
| 193 |
int height = pixels.getHeight(); |
| 194 |
|
| 195 |
if (width > MAX_THUMBNAIL_DIMENSION || height > MAX_THUMBNAIL_DIMENSION) { |
| 196 |
throw new IllegalArgumentException("Thumbnail dimensions width,height (" + width + |
| 197 |
"," + height + ") too large, dimensions must be smaller than " + |
| 198 |
MAX_THUMBNAIL_DIMENSION); |
| 199 |
} |
| 200 |
|
| 201 |
ByteBuffer rgbBuffer = convertToRGB(pixels); |
| 202 |
nativeSetThumbnail(rgbBuffer, width, height); |
| 203 |
|
| 204 |
return this; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
@NonNull |
| 223 |
public DngCreator setThumbnail(@NonNull Image pixels) { |
| 224 |
if (pixels == null) { |
| 225 |
throw new IllegalArgumentException("Null argument to setThumbnail"); |
| 226 |
} |
| 227 |
|
| 228 |
int format = pixels.getFormat(); |
| 229 |
if (format != ImageFormat.YUV_420_888) { |
| 230 |
throw new IllegalArgumentException("Unsupported Image format " + format); |
| 231 |
} |
| 232 |
|
| 233 |
int width = pixels.getWidth(); |
| 234 |
int height = pixels.getHeight(); |
| 235 |
|
| 236 |
if (width > MAX_THUMBNAIL_DIMENSION || height > MAX_THUMBNAIL_DIMENSION) { |
| 237 |
throw new IllegalArgumentException("Thumbnail dimensions width,height (" + width + |
| 238 |
"," + height + ") too large, dimensions must be smaller than " + |
| 239 |
MAX_THUMBNAIL_DIMENSION); |
| 240 |
} |
| 241 |
|
| 242 |
ByteBuffer rgbBuffer = convertToRGB(pixels); |
| 243 |
nativeSetThumbnail(rgbBuffer, width, height); |
| 244 |
|
| 245 |
return this; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
@NonNull |
| 265 |
public DngCreator setLocation(@NonNull Location location) { |
| 266 |
if (location == null) { |
| 267 |
throw new IllegalArgumentException("Null location passed to setLocation"); |
| 268 |
} |
| 269 |
double latitude = location.getLatitude(); |
| 270 |
double longitude = location.getLongitude(); |
| 271 |
long time = location.getTime(); |
| 272 |
|
| 273 |
int[] latTag = toExifLatLong(latitude); |
| 274 |
int[] longTag = toExifLatLong(longitude); |
| 275 |
String latRef = latitude >= 0 ? GPS_LAT_REF_NORTH : GPS_LAT_REF_SOUTH; |
| 276 |
String longRef = longitude >= 0 ? GPS_LONG_REF_EAST : GPS_LONG_REF_WEST; |
| 277 |
|
| 278 |
String dateTag = sExifGPSDateStamp.format(time); |
| 279 |
mGPSTimeStampCalendar.setTimeInMillis(time); |
| 280 |
int[] timeTag = new int[] { mGPSTimeStampCalendar.get(Calendar.HOUR_OF_DAY), 1, |
| 281 |
mGPSTimeStampCalendar.get(Calendar.MINUTE), 1, |
| 282 |
mGPSTimeStampCalendar.get(Calendar.SECOND), 1 }; |
| 283 |
nativeSetGpsTags(latTag, latRef, longTag, longRef, dateTag, timeTag); |
| 284 |
return this; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
@NonNull |
| 298 |
public DngCreator setDescription(@NonNull String description) { |
| 299 |
if (description == null) { |
| 300 |
throw new IllegalArgumentException("Null description passed to setDescription."); |
| 301 |
} |
| 302 |
nativeSetDescription(description); |
| 303 |
return this; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
public void writeInputStream(@NonNull OutputStream dngOutput, @NonNull Size size, |
| 336 |
@NonNull InputStream pixels, @IntRange(from=0) long offset) throws IOException { |
| 337 |
if (dngOutput == null) { |
| 338 |
throw new IllegalArgumentException("Null dngOutput passed to writeInputStream"); |
| 339 |
} else if (size == null) { |
| 340 |
throw new IllegalArgumentException("Null size passed to writeInputStream"); |
| 341 |
} else if (pixels == null) { |
| 342 |
throw new IllegalArgumentException("Null pixels passed to writeInputStream"); |
| 343 |
} else if (offset < 0) { |
| 344 |
throw new IllegalArgumentException("Negative offset passed to writeInputStream"); |
| 345 |
} |
| 346 |
|
| 347 |
int width = size.getWidth(); |
| 348 |
int height = size.getHeight(); |
| 349 |
if (width <= 0 || height <= 0) { |
| 350 |
throw new IllegalArgumentException("Size with invalid width, height: (" + width + "," + |
| 351 |
height + ") passed to writeInputStream"); |
| 352 |
} |
| 353 |
nativeWriteInputStream(dngOutput, pixels, width, height, offset); |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
public void writeByteBuffer(@NonNull OutputStream dngOutput, @NonNull Size size, |
| 390 |
@NonNull ByteBuffer pixels, @IntRange(from=0) long offset) |
| 391 |
throws IOException { |
| 392 |
if (dngOutput == null) { |
| 393 |
throw new IllegalArgumentException("Null dngOutput passed to writeByteBuffer"); |
| 394 |
} else if (size == null) { |
| 395 |
throw new IllegalArgumentException("Null size passed to writeByteBuffer"); |
| 396 |
} else if (pixels == null) { |
| 397 |
throw new IllegalArgumentException("Null pixels passed to writeByteBuffer"); |
| 398 |
} else if (offset < 0) { |
| 399 |
throw new IllegalArgumentException("Negative offset passed to writeByteBuffer"); |
| 400 |
} |
| 401 |
|
| 402 |
int width = size.getWidth(); |
| 403 |
int height = size.getHeight(); |
| 404 |
|
| 405 |
writeByteBuffer(width, height, pixels, dngOutput, DEFAULT_PIXEL_STRIDE, |
| 406 |
width * DEFAULT_PIXEL_STRIDE, offset); |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
public void writeImage(@NonNull OutputStream dngOutput, @NonNull Image pixels) |
| 427 |
throws IOException { |
| 428 |
if (dngOutput == null) { |
| 429 |
throw new IllegalArgumentException("Null dngOutput to writeImage"); |
| 430 |
} else if (pixels == null) { |
| 431 |
throw new IllegalArgumentException("Null pixels to writeImage"); |
| 432 |
} |
| 433 |
|
| 434 |
int format = pixels.getFormat(); |
| 435 |
if (format != ImageFormat.RAW_SENSOR) { |
| 436 |
throw new IllegalArgumentException("Unsupported image format " + format); |
| 437 |
} |
| 438 |
|
| 439 |
Image.Plane[] planes = pixels.getPlanes(); |
| 440 |
if (planes == null || planes.length <= 0) { |
| 441 |
throw new IllegalArgumentException("Image with no planes passed to writeImage"); |
| 442 |
} |
| 443 |
|
| 444 |
ByteBuffer buf = planes[0].getBuffer(); |
| 445 |
writeByteBuffer(pixels.getWidth(), pixels.getHeight(), buf, dngOutput, |
| 446 |
planes[0].getPixelStride(), planes[0].getRowStride(), 0); |
| 447 |
} |
| 448 |
|
| 449 |
@Override |
| 450 |
public void close() { |
| 451 |
nativeDestroy(); |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
public static final int MAX_THUMBNAIL_DIMENSION = 256; |
| 458 |
|
| 459 |
@Override |
| 460 |
protected void finalize() throws Throwable { |
| 461 |
try { |
| 462 |
close(); |
| 463 |
} finally { |
| 464 |
super.finalize(); |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
private static final String GPS_LAT_REF_NORTH = "N"; |
| 469 |
private static final String GPS_LAT_REF_SOUTH = "S"; |
| 470 |
private static final String GPS_LONG_REF_EAST = "E"; |
| 471 |
private static final String GPS_LONG_REF_WEST = "W"; |
| 472 |
|
| 473 |
private static final String GPS_DATE_FORMAT_STR = "yyyy:MM:dd"; |
| 474 |
private static final String TIFF_DATETIME_FORMAT = "yyyy:MM:dd HH:mm:ss"; |
| 475 |
private static final DateFormat sExifGPSDateStamp = |
| 476 |
new SimpleDateFormat(GPS_DATE_FORMAT_STR, Locale.US); |
| 477 |
private final Calendar mGPSTimeStampCalendar = Calendar |
| 478 |
.getInstance(TimeZone.getTimeZone("UTC")); |
| 479 |
|
| 480 |
static { |
| 481 |
sExifGPSDateStamp.setTimeZone(TimeZone.getTimeZone("UTC")); |
| 482 |
} |
| 483 |
|
| 484 |
private static final int DEFAULT_PIXEL_STRIDE = 2; |
| 485 |
private static final int BYTES_PER_RGB_PIX = 3; |
| 486 |
|
| 487 |
|
| 488 |
private static final int TAG_ORIENTATION_UNKNOWN = 9; |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
private void writeByteBuffer(int width, int height, ByteBuffer pixels, OutputStream dngOutput, |
| 494 |
int pixelStride, int rowStride, long offset) throws IOException { |
| 495 |
if (width <= 0 || height <= 0) { |
| 496 |
throw new IllegalArgumentException("Image with invalid width, height: (" + width + "," + |
| 497 |
height + ") passed to write"); |
| 498 |
} |
| 499 |
long capacity = pixels.capacity(); |
| 500 |
long totalSize = ((long) rowStride) * height + offset; |
| 501 |
if (capacity < totalSize) { |
| 502 |
throw new IllegalArgumentException("Image size " + capacity + |
| 503 |
" is too small (must be larger than " + totalSize + ")"); |
| 504 |
} |
| 505 |
int minRowStride = pixelStride * width; |
| 506 |
if (minRowStride > rowStride) { |
| 507 |
throw new IllegalArgumentException("Invalid image pixel stride, row byte width " + |
| 508 |
minRowStride + " is too large, expecting " + rowStride); |
| 509 |
} |
| 510 |
pixels.clear(); |
| 511 |
nativeWriteImage(dngOutput, width, height, pixels, rowStride, pixelStride, offset, |
| 512 |
pixels.isDirect()); |
| 513 |
pixels.clear(); |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
private static void yuvToRgb(byte[] yuvData, int outOffset, byte[] rgbOut) { |
| 520 |
final int COLOR_MAX = 255; |
| 521 |
|
| 522 |
float y = yuvData[0] & 0xFF; |
| 523 |
float cb = yuvData[1] & 0xFF; |
| 524 |
float cr = yuvData[2] & 0xFF; |
| 525 |
|
| 526 |
|
| 527 |
float r = y + 1.402f * (cr - 128); |
| 528 |
float g = y - 0.34414f * (cb - 128) - 0.71414f * (cr - 128); |
| 529 |
float b = y + 1.772f * (cb - 128); |
| 530 |
|
| 531 |
|
| 532 |
rgbOut[outOffset] = (byte) Math.max(0, Math.min(COLOR_MAX, r)); |
| 533 |
rgbOut[outOffset + 1] = (byte) Math.max(0, Math.min(COLOR_MAX, g)); |
| 534 |
rgbOut[outOffset + 2] = (byte) Math.max(0, Math.min(COLOR_MAX, b)); |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
private static void colorToRgb(int color, int outOffset, byte[] rgbOut) { |
| 541 |
rgbOut[outOffset] = (byte) Color.red(color); |
| 542 |
rgbOut[outOffset + 1] = (byte) Color.green(color); |
| 543 |
rgbOut[outOffset + 2] = (byte) Color.blue(color); |
| 544 |
|
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
private static ByteBuffer convertToRGB(Image yuvImage) { |
| 551 |
|
| 552 |
int width = yuvImage.getWidth(); |
| 553 |
int height = yuvImage.getHeight(); |
| 554 |
ByteBuffer buf = ByteBuffer.allocateDirect(BYTES_PER_RGB_PIX * width * height); |
| 555 |
|
| 556 |
Image.Plane yPlane = yuvImage.getPlanes()[0]; |
| 557 |
Image.Plane uPlane = yuvImage.getPlanes()[1]; |
| 558 |
Image.Plane vPlane = yuvImage.getPlanes()[2]; |
| 559 |
|
| 560 |
ByteBuffer yBuf = yPlane.getBuffer(); |
| 561 |
ByteBuffer uBuf = uPlane.getBuffer(); |
| 562 |
ByteBuffer vBuf = vPlane.getBuffer(); |
| 563 |
|
| 564 |
yBuf.rewind(); |
| 565 |
uBuf.rewind(); |
| 566 |
vBuf.rewind(); |
| 567 |
|
| 568 |
int yRowStride = yPlane.getRowStride(); |
| 569 |
int vRowStride = vPlane.getRowStride(); |
| 570 |
int uRowStride = uPlane.getRowStride(); |
| 571 |
|
| 572 |
int yPixStride = yPlane.getPixelStride(); |
| 573 |
int vPixStride = vPlane.getPixelStride(); |
| 574 |
int uPixStride = uPlane.getPixelStride(); |
| 575 |
|
| 576 |
byte[] yuvPixel = { 0, 0, 0 }; |
| 577 |
byte[] yFullRow = new byte[yPixStride * (width - 1) + 1]; |
| 578 |
byte[] uFullRow = new byte[uPixStride * (width / 2 - 1) + 1]; |
| 579 |
byte[] vFullRow = new byte[vPixStride * (width / 2 - 1) + 1]; |
| 580 |
byte[] finalRow = new byte[BYTES_PER_RGB_PIX * width]; |
| 581 |
for (int i = 0; i < height; i++) { |
| 582 |
int halfH = i / 2; |
| 583 |
yBuf.position(yRowStride * i); |
| 584 |
yBuf.get(yFullRow); |
| 585 |
uBuf.position(uRowStride * halfH); |
| 586 |
uBuf.get(uFullRow); |
| 587 |
vBuf.position(vRowStride * halfH); |
| 588 |
vBuf.get(vFullRow); |
| 589 |
for (int j = 0; j < width; j++) { |
| 590 |
int halfW = j / 2; |
| 591 |
yuvPixel[0] = yFullRow[yPixStride * j]; |
| 592 |
yuvPixel[1] = uFullRow[uPixStride * halfW]; |
| 593 |
yuvPixel[2] = vFullRow[vPixStride * halfW]; |
| 594 |
yuvToRgb(yuvPixel, j * BYTES_PER_RGB_PIX, finalRow); |
| 595 |
} |
| 596 |
buf.put(finalRow); |
| 597 |
} |
| 598 |
|
| 599 |
yBuf.rewind(); |
| 600 |
uBuf.rewind(); |
| 601 |
vBuf.rewind(); |
| 602 |
buf.rewind(); |
| 603 |
return buf; |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
private static ByteBuffer convertToRGB(Bitmap argbBitmap) { |
| 610 |
|
| 611 |
int width = argbBitmap.getWidth(); |
| 612 |
int height = argbBitmap.getHeight(); |
| 613 |
ByteBuffer buf = ByteBuffer.allocateDirect(BYTES_PER_RGB_PIX * width * height); |
| 614 |
|
| 615 |
int[] pixelRow = new int[width]; |
| 616 |
byte[] finalRow = new byte[BYTES_PER_RGB_PIX * width]; |
| 617 |
for (int i = 0; i < height; i++) { |
| 618 |
argbBitmap.getPixels(pixelRow, 0, width, 0, i, |
| 619 |
width, 1); |
| 620 |
for (int j = 0; j < width; j++) { |
| 621 |
colorToRgb(pixelRow[j], j * BYTES_PER_RGB_PIX, finalRow); |
| 622 |
} |
| 623 |
buf.put(finalRow); |
| 624 |
} |
| 625 |
|
| 626 |
buf.rewind(); |
| 627 |
return buf; |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
private static int[] toExifLatLong(double value) { |
| 634 |
|
| 635 |
value = Math.abs(value); |
| 636 |
int degrees = (int) value; |
| 637 |
value = (value - degrees) * 60; |
| 638 |
int minutes = (int) value; |
| 639 |
value = (value - minutes) * 6000; |
| 640 |
int seconds = (int) value; |
| 641 |
return new int[] { degrees, 1, minutes, 1, seconds, 100 }; |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
private long mNativeContext; |
| 648 |
|
| 649 |
private static native void nativeClassInit(); |
| 650 |
|
| 651 |
private synchronized native void nativeInit(CameraMetadataNative nativeCharacteristics, |
| 652 |
CameraMetadataNative nativeResult, |
| 653 |
String captureTime); |
| 654 |
|
| 655 |
private synchronized native void nativeDestroy(); |
| 656 |
|
| 657 |
private synchronized native void nativeSetOrientation(int orientation); |
| 658 |
|
| 659 |
private synchronized native void nativeSetDescription(String description); |
| 660 |
|
| 661 |
private synchronized native void nativeSetGpsTags(int[] latTag, String latRef, int[] longTag, |
| 662 |
String longRef, String dateTag, |
| 663 |
int[] timeTag); |
| 664 |
|
| 665 |
private synchronized native void nativeSetThumbnail(ByteBuffer buffer, int width, int height); |
| 666 |
|
| 667 |
private synchronized native void nativeWriteImage(OutputStream out, int width, int height, |
| 668 |
ByteBuffer rawBuffer, int rowStride, |
| 669 |
int pixStride, long offset, boolean isDirect) |
| 670 |
throws IOException; |
| 671 |
|
| 672 |
private synchronized native void nativeWriteInputStream(OutputStream out, InputStream rawStream, |
| 673 |
int width, int height, long offset) |
| 674 |
throws IOException; |
| 675 |
|
| 676 |
static { |
| 677 |
nativeClassInit(); |
| 678 |
} |
| 679 |
} |