Java Code Examples for android.media.Image#getFormat()

The following examples show how to use android.media.Image#getFormat() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DngCreator.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Set the thumbnail image.
 *
 * <p>
 * Pixel data is interpreted as a {@link android.graphics.ImageFormat#YUV_420_888} image.
 * Thumbnail images with a dimension larger than {@link #MAX_THUMBNAIL_DIMENSION} will be
 * rejected.
 * </p>
 *
 * @param pixels an {@link android.media.Image} object with the format
 *               {@link android.graphics.ImageFormat#YUV_420_888}.
 * @return this {@link #DngCreator} object.
 * @throws java.lang.IllegalArgumentException if the given thumbnail image has a dimension
 *      larger than {@link #MAX_THUMBNAIL_DIMENSION}.
 */
@NonNull
public DngCreator setThumbnail(@NonNull Image pixels) {
    if (pixels == null) {
        throw new IllegalArgumentException("Null argument to setThumbnail");
    }

    int format = pixels.getFormat();
    if (format != ImageFormat.YUV_420_888) {
        throw new IllegalArgumentException("Unsupported Image format " + format);
    }

    int width = pixels.getWidth();
    int height = pixels.getHeight();

    if (width > MAX_THUMBNAIL_DIMENSION || height > MAX_THUMBNAIL_DIMENSION) {
        throw new IllegalArgumentException("Thumbnail dimensions width,height (" + width +
                "," + height + ") too large, dimensions must be smaller than " +
                MAX_THUMBNAIL_DIMENSION);
    }

    ByteBuffer rgbBuffer = convertToRGB(pixels);
    nativeSetThumbnail(rgbBuffer, width, height);

    return this;
}
 
Example 2
Source File: DngCreator.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Write the pixel data to a DNG file with the currently configured metadata.
 *
 * <p>
 * For this method to succeed, the {@link android.media.Image} input must contain
 * {@link android.graphics.ImageFormat#RAW_SENSOR} pixel data, otherwise an
 * {@link java.lang.IllegalArgumentException} will be thrown.
 * </p>
 *
 * @param dngOutput an {@link java.io.OutputStream} to write the DNG file to.
 * @param pixels an {@link android.media.Image} to write.
 *
 * @throws java.io.IOException if an error was encountered in the output stream.
 * @throws java.lang.IllegalArgumentException if an image with an unsupported format was used.
 * @throws java.lang.IllegalStateException if not enough metadata information has been
 *          set to write a well-formatted DNG file.
 */
public void writeImage(@NonNull OutputStream dngOutput, @NonNull Image pixels)
        throws IOException {
    if (dngOutput == null) {
        throw new IllegalArgumentException("Null dngOutput to writeImage");
    } else if (pixels == null) {
        throw new IllegalArgumentException("Null pixels to writeImage");
    }

    int format = pixels.getFormat();
    if (format != ImageFormat.RAW_SENSOR) {
        throw new IllegalArgumentException("Unsupported image format " + format);
    }

    Image.Plane[] planes = pixels.getPlanes();
    if (planes == null || planes.length <= 0) {
        throw new IllegalArgumentException("Image with no planes passed to writeImage");
    }

    ByteBuffer buf = planes[0].getBuffer();
    writeByteBuffer(pixels.getWidth(), pixels.getHeight(), buf, dngOutput,
            planes[0].getPixelStride(), planes[0].getRowStride(), 0);
}
 
Example 3
Source File: HeifReader.java    From heifreader with MIT License 6 votes vote down vote up
private static Bitmap renderHevcImageWithFormat(ByteBuffer bitstream, ImageInfo info, int imageFormat) throws FormatFallbackException {
    try (ImageReader reader = ImageReader.newInstance(info.size.getWidth(), info.size.getHeight(), imageFormat, 1)) {
        renderHevcImage(bitstream, info, reader.getSurface());
        Image image = null;
        try {
            try {
                image = reader.acquireNextImage();
            } catch (UnsupportedOperationException ex) {
                throw new FormatFallbackException(ex);
            }

            switch (image.getFormat()) {
                case ImageFormat.YUV_420_888:
                case ImageFormat.YV12:
                    return convertYuv420ToBitmap(image);
                case ImageFormat.RGB_565:
                    return convertRgb565ToBitmap(image);
                default:
                    throw new RuntimeException("unsupported image format(" + image.getFormat() + ")");
            }
        } finally {
            if (image != null) {
                image.close();
            }
        }
    }
}
 
Example 4
Source File: ImageDecoder.java    From FastBarcodeScanner with Apache License 2.0 6 votes vote down vote up
public static byte[] Serialize(Image image)
{
    if (image==null)
        return null;

    Image.Plane[] planes = image.getPlanes();

    // NV21 expects planes in order YVU, not YUV:
    if (image.getFormat() == ImageFormat.YUV_420_888)
        planes = new Image.Plane[] {planes[0], planes[2], planes[1]};

    byte[] serializeBytes = new byte[getSerializedSize(image)];
    int nextFree = 0;

    for (Image.Plane plane: planes)
    {
        ByteBuffer buffer = plane.getBuffer();
        buffer.position(0);
        int nBytes = buffer.remaining();
        plane.getBuffer().get(serializeBytes, nextFree, nBytes);
        nextFree += nBytes;
    }

    return serializeBytes;
}
 
Example 5
Source File: OneCameraImpl.java    From Camera2 with Apache License 2.0 5 votes vote down vote up
/**
 * Given an image reader, this extracts the final image. If the image in the
 * reader is JPEG, we extract and return it as is. If the image is YUV, we
 * convert it to JPEG and return the result.
 *
 * @param image the image we got from the image reader.
 * @return A valid JPEG image.
 */
private static byte[] acquireJpegBytesAndClose(Image image)
{
    ByteBuffer buffer;
    if (image.getFormat() == ImageFormat.JPEG)
    {
        Image.Plane plane0 = image.getPlanes()[0];
        buffer = plane0.getBuffer();
    } else if (image.getFormat() == ImageFormat.YUV_420_888)
    {
        buffer = ByteBuffer.allocateDirect(image.getWidth() * image.getHeight() * 3);

        Log.v(TAG, "Compressing JPEG with software encoder.");
        int numBytes = JpegUtilNative.compressJpegFromYUV420Image(new AndroidImageProxy(image), buffer,
                JPEG_QUALITY);

        if (numBytes < 0)
        {
            throw new RuntimeException("Error compressing jpeg.");
        }
        buffer.limit(numBytes);
    } else
    {
        throw new RuntimeException("Unsupported image format.");
    }

    byte[] imageBytes = new byte[buffer.remaining()];
    buffer.get(imageBytes);
    buffer.rewind();
    image.close();
    return imageBytes;
}
 
Example 6
Source File: ImageUtil.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * {@link Image} を JPEG のバイナリデータに変換します.
 *
 * @param image 元の画像
 * @return JPEG のバイナリデータ
 */
public static byte[] convertToJPEG(Image image) {
    byte[] jpeg;
    if (image.getFormat() == ImageFormat.JPEG) {
        jpeg = readJPEG(image);
    } else if (image.getFormat() == ImageFormat.YUV_420_888) {
        jpeg = NV21toJPEG(YUV420toNV21(image), image.getWidth(), image.getHeight(), 100);
    } else {
        throw new RuntimeException("Unsupported format: " + image.getFormat());
    }
    return jpeg;
}
 
Example 7
Source File: ResultProcessor.java    From libsoftwaresync with Apache License 2.0 4 votes vote down vote up
private void processStill(final Frame frame, String basename) {
  File captureDir = new File(context.getExternalFilesDir(null), basename);
  if (!captureDir.exists() && !captureDir.mkdirs()) {
    throw new IllegalStateException("Could not create dir " + captureDir);
  }
  // Timestamp in local domain ie. time since boot in nanoseconds.
  long localSensorTimestampNs = frame.result.get(CaptureResult.SENSOR_TIMESTAMP);
  // Timestamp in leader domain ie. synchronized time on leader device in nanoseconds.
  long syncedSensorTimestampNs =
      timeDomainConverter.leaderTimeForLocalTimeNs(localSensorTimestampNs);
  // Use syncedSensorTimestamp in milliseconds for filenames.
  long syncedSensorTimestampMs = (long) TimeUtils.nanosToMillis(syncedSensorTimestampNs);
  String filenameTimeString = getTimeStr(syncedSensorTimestampMs);

  // Save timing metadata.
  {
    String metaFilename = "sync_metadata_" + filenameTimeString + ".txt";
    File metaFile = new File(captureDir, metaFilename);
    saveTimingMetadata(syncedSensorTimestampNs, localSensorTimestampNs, metaFile);
  }

  for (int i = 0; i < frame.output.images.size(); ++i) {
    Image image = frame.output.images.get(i);
    int format = image.getFormat();
    if (format == ImageFormat.RAW_SENSOR) {
      // Note: while using DngCreator works, streaming RAW_SENSOR is too slow.
      Log.e(TAG, "RAW_SENSOR saving not implemented!");
    } else if (format == ImageFormat.JPEG) {
      Log.e(TAG, "JPEG saving not implemented!");
    } else if (format == ImageFormat.RAW10) {
      Log.e(TAG, "RAW10 saving not implemented!");
    } else if (format == ImageFormat.YUV_420_888) {
      // TODO(jiawen): We know that on Pixel devices, the YUV format is NV21, consisting of a luma
      // plane and separate interleaved chroma planes.
      //     <--w-->
      // ^   YYYYYYYZZZ
      // |   YYYYYYYZZZ
      // h   ...
      // |   ...
      // v   YYYYYYYZZZ
      //
      //     <--w-->
      // ^   VUVUVUVZZZZZ
      // |   VUVUVUVZZZZZ
      // h/2 ...
      // |   ...
      // v   VUVUVUVZZZZZ
      //
      // where Z is padding bytes.
      //
      // TODO(jiawen): To determine if it's NV12 vs NV21, we need JNI to compare the buffer start
      // addresses.

      context.notifyCapturing("img_" + filenameTimeString);

      // Save NV21 raw + metadata.
      {
        File nv21File = new File(captureDir, "img_" + filenameTimeString + ".nv21");
        File nv21MetadataFile =
            new File(captureDir, "nv21_metadata_" + filenameTimeString + ".txt");
        saveNv21(image, nv21File, nv21MetadataFile);
        context.notifyCaptured(nv21File.getName());
      }

      // TODO(samansari): Make save JPEG a checkbox in the UI.
      if (saveJpgFromNv21) {
        YuvImage yuvImage = yuvImageFromNv21Image(image);
        File jpgFile = new File(captureDir, "img_" + filenameTimeString + ".jpg");

        // Push saving JPEG onto queue to let the frame close faster, necessary for some devices.
        handler.post(() -> saveJpg(yuvImage, jpgFile));
      }
    } else {
      Log.e(TAG, String.format("Cannot save unsupported image format: %d", image.getFormat()));
    }
  }

  frame.close();
}