[Java] Mit Apathce Tika Metadaten aus Dateien abrufen und Breite und Höhe von Bildern / Videos aus Metadaten abrufen [Kotlin]

Was du machen willst

Ermitteln Sie die Bildgröße (Breite und Höhe) des Bilds / Videos.

In diesem Artikel verwenden wir Apache Tika, um die Metadaten abzurufen und dann die Frame-Größe aus diesen Metadaten abzurufen.

Entschuldigung

Soweit ich das obige offizielle Dokument gelesen habe, kann die in diesem Artikel vorgestellte Methode nur Bildformate (+ JPEG, soweit ich es versucht habe) unterstützen, die von Java-Plattformen wie PNG, GIF, BMP und Videos im Format FLV, MP4 unterstützt werden. .. Dies ändert sich auch dann nicht, wenn "Inhaltstyp" als "Video / mp4" behandelt wird, z. B. "mov", das im später beschriebenen Ausführungsergebnis angezeigt wird.

Als ich recherchierte, schien es, dass es bisher fast keine Möglichkeit gibt, die Frame-Größe in Java zu ermitteln (ich würde es begrüßen, wenn Sie es mir sagen könnten). Wenn Sie es sicher tun möchten, ist es meiner Meinung nach besser, ffmpeg von Java aus aufzurufen.

Einführungsmethode

Stellen Sie Apache Tika Parsers von Maven vor. Ich habe 1.21 zur Verifizierung verwendet.

Apache Tika Parsers

Verifizierungs-Schlüssel

Es ist ein grobes Beispiel, aber es ist ein Beispiel, um die Bildgröße für alle Dateien unter "src / main / resources" zu ermitteln. Es ist nach Erweiterung und Ausgabe sortiert.

Der org.apache.tika.metadata.Metadata-Typ wird benannt, weil er als Kotlins Metadatentyp bezeichnet wird.

import org.apache.tika.Tika
import org.apache.tika.io.TikaInputStream
import java.io.File
import org.apache.tika.metadata.Metadata as TikaMetadata

fun main() {
    val tika = Tika()

    File(System.getProperty("user.dir") + "/src/main/resources").listFiles().filter {
        it.name != ".gitkeep"
    }.map {
        val metaData = TikaMetadata() //Wenn Sie nicht jede Datei deklarieren, bleiben die zuletzt bewerteten Daten endlos.
        val tikaStream = TikaInputStream.get(it.toURI(), metaData)

        tika.parse(tikaStream, metaData)

        val fileInfo = """
            Extension: ${metaData.get("resourceName").split(".").last().toLowerCase()}
            Content-Type = ${metaData.get("Content-Type")}
        """.trimIndent()

        val width = metaData.get("tiff:ImageWidth") ?.let { w -> Integer.parseInt(w) }
        val height = metaData.get("tiff:ImageLength") ?.let { h -> Integer.parseInt(h) }

        """
            #$fileInfo
            #width: ${width?: "nothing" }
            #height: ${height?: "nothing"}
        """.trimMargin("#")
    }.sorted().forEach {
        println(it)
        println("----------------------")
    }
}

Ausführungsergebnis

Dies ist das Ergebnis des Einfügens der aufgenommenen Daten in den Text.

Es ist lang und daher gefaltet
Extension: 7z
Content-Type = application/x-7z-compressed
width: nothing
height: nothing
----------------------
Extension: avi
Content-Type = video/x-msvideo
width: nothing
height: nothing
----------------------
Extension: docx
Content-Type = application/vnd.openxmlformats-officedocument.wordprocessingml.document
width: nothing
height: nothing
----------------------
Extension: exe
Content-Type = application/x-msdownload
width: nothing
height: nothing
----------------------
Extension: flv
Content-Type = video/x-flv
width: nothing
height: nothing
----------------------
Extension: gif
Content-Type = image/gif
width: 300
height: 250
----------------------
Extension: gif
Content-Type = image/gif
width: 300
height: 250
----------------------
Extension: gif
Content-Type = image/gif
width: 300
height: 250
----------------------
Extension: html
Content-Type = text/html; charset=Shift_JIS
width: nothing
height: nothing
----------------------
Extension: jpg
Content-Type = image/jpeg
width: 4608
height: 2592
----------------------
Extension: jpg
Content-Type = image/jpeg
width: 759
height: 1074
----------------------
Extension: m3u
Content-Type = text/plain; charset=windows-1252
width: nothing
height: nothing
----------------------
Extension: mkv
Content-Type = video/x-matroska
width: nothing
height: nothing
----------------------
Extension: mkv
Content-Type = video/x-matroska
width: nothing
height: nothing
----------------------
Extension: mkv
Content-Type = video/x-matroska
width: nothing
height: nothing
----------------------
Extension: mkv
Content-Type = video/x-matroska
width: nothing
height: nothing
----------------------
Extension: mov
Content-Type = application/mp4
width: 0
height: 0
----------------------
Extension: mov
Content-Type = application/mp4
width: 0
height: 0
----------------------
Extension: mov
Content-Type = video/quicktime
width: 0
height: 0
----------------------
Extension: mov
Content-Type = video/quicktime
width: 320
height: 240
----------------------
Extension: mp3
Content-Type = audio/mpeg
width: nothing
height: nothing
----------------------
Extension: mp4
Content-Type = application/mp4
width: 320
height: 240
----------------------
Extension: mp4
Content-Type = application/mp4
width: 640
height: 360
----------------------
Extension: mp4
Content-Type = application/mp4
width: 725
height: 408
----------------------
Extension: mp4
Content-Type = application/mp4
width: 725
height: 408
----------------------
Extension: mp4
Content-Type = video/mp4
width: 0
height: 0
----------------------
Extension: mp4
Content-Type = video/mp4
width: 0
height: 0
----------------------
Extension: mp4
Content-Type = video/mp4
width: 718
height: 404
----------------------
Extension: mp4
Content-Type = video/mp4
width: 720
height: 408
----------------------
Extension: mp4
Content-Type = video/mp4
width: 854
height: 480
----------------------
Extension: mp4
Content-Type = video/x-m4v
width: 725
height: 408
----------------------
Extension: mpg
Content-Type = video/mpeg
width: nothing
height: nothing
----------------------
Extension: mpg
Content-Type = video/mpeg
width: nothing
height: nothing
----------------------
Extension: mpg
Content-Type = video/mpeg
width: nothing
height: nothing
----------------------
Extension: msi
Content-Type = application/x-ms-installer
width: nothing
height: nothing
----------------------
Extension: pdf
Content-Type = application/pdf
width: nothing
height: nothing
----------------------
Extension: png
Content-Type = image/png
width: 600
height: 371
----------------------
Extension: pptx
Content-Type = application/vnd.openxmlformats-officedocument.presentationml.presentation
width: nothing
height: nothing
----------------------
Extension: svg
Content-Type = image/svg+xml
width: nothing
height: nothing
----------------------
Extension: ts
Content-Type = application/octet-stream
width: nothing
height: nothing
----------------------
Extension: vcmf
Content-Type = application/octet-stream
width: nothing
height: nothing
----------------------
Extension: vob
Content-Type = video/mpeg
width: nothing
height: nothing
----------------------
Extension: webm
Content-Type = video/webm
width: nothing
height: nothing
----------------------
Extension: webm
Content-Type = video/webm
width: nothing
height: nothing
----------------------
Extension: webm
Content-Type = video/webm
width: nothing
height: nothing
----------------------
Extension: webm
Content-Type = video/webm
width: nothing
height: nothing
----------------------
Extension: wmv
Content-Type = video/x-ms-wmv
width: nothing
height: nothing
----------------------
Extension: zip
Content-Type = application/zip
width: nothing
height: nothing
----------------------

Recommended Posts

[Java] Mit Apathce Tika Metadaten aus Dateien abrufen und Breite und Höhe von Bildern / Videos aus Metadaten abrufen [Kotlin]
[Java] Zeichensatz mit Apathce Tika abrufen / String von Zeichensatz initialisieren [Kotlin]
[Java] Holen Sie sich MimeType aus dem Inhalt der Datei mit Apathce Tika [Kotlin]
[Java] Analysieren Sie Excel-Dateien (nicht auf verschiedene Dateien beschränkt) mit Apathce Tika [Kotlin]
[Kotlin] Holen Sie sich Java Constructor / Method von KFunction und rufen Sie es auf
[Java] Lesen und Schreiben von Dateien mit OpenCSV
[Java] Tag-Informationen aus Musikdateien abrufen
[Kotlin] Doppelte Dateien löschen [Java]
Reverse Assemble / Reverse Compile .Klassendatei aus Kotlin kompiliert
[Java] KFunction von Method / Constructor in Java abrufen [Kotlin]
[Review] Lesen und Schreiben von Dateien mit Java (JDK6)
[Android-Entwicklung] Holen Sie sich mit Java Bilder vom Server und legen Sie sie in ImageView fest! !!
Rufen Sie eine Methode mit Kotlins Rückrufblock von Java aus auf
Java-Sprache aus der Sicht von Kotlin und C #
Ich möchte Bildschirmübergänge mit Kotlin und Java machen!
[Java] Entwicklung mit mehreren Dateien mittels Paket und Import