English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Android-Bildverarbeitungs-Toolklasse BitmapUtils

Android Bildverarbeitungs-Utilitätsklasse BitmapUtils, zur Verfügung gestellt, detaillierte Inhalte wie folgt

In Projekten wird oft das Bild verwendet, daher werde ich hier eine einfache Zusammenfassung vornehmen. Reden wir weniger, hier ist der Code.

package com.lvstudio.myapp.utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
 * Erstellt von LvStudio am 2016/11/7.
 */
public class BitmapUtils {
  /**
   * Methode zur Komprimierung von Bildern mit Bildschirmauflösung und angegebener Klarheit
   *
   * @param context
   * @param image Bitmap-Bild
   * @return
   */
  public static Bitmap comp(Context context, Bitmap image) {
    int maxLength = 1024 * 1024; // Vorgesehene maximale Bildspeichergröße, Einheit byte
    // Komprimierte Größe
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// Qualitätskomprimierungsmodus, hier100 bedeutet, nicht zu komprimieren, speichert die komprimierten Daten in baos
    int options = 100;
    while (baos.toByteArray().length > maxLength) { // Schleifenbeurteilung, weiter komprimieren, wenn größer
      options -= 10;// Jedes Mal wird reduziert10
      baos.reset();// Setzt baos zurück, d.h. leert baos
      image.compress(Bitmap.CompressFormat.JPEG, options, baos);//PNG-Komprimierungsoptionen%
    }
    // Komprimierte Größe
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    BitmapFactory.Options opts = new BitmapFactory.Options(); // Optionenobjekt (wird beim Laden des Bildes verwendet)
    opts.inJustDecodeBounds = true; // Optionen ändern, nur Größe erhalten
    BitmapFactory.decodeStream(bais, null, opts);// Laden des Bildes (nur Bildgröße erhalten)
    // Bildschirmgröße erhalten, nach Verhältnis komprimieren
    WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    int SkalierungX = opts.outWidth / manager.getDefaultDisplay().getWidth(); // X-Achsenverhältnis der Skalierung (Bildbreite/Bildschirmbreite)
    int SkalierungY = opts.outHeight / manager.getDefaultDisplay().getHeight(); // Y-Achsenverhältnis der Skalierung
    int scale = SkalierungX > SkalierungY &63; SkalierungX : SkalierungY; // Verhältnis der Bildskalierung (welches von X und Y auswählen, was zuerst gewählt wird)
    opts.inJustDecodeBounds = false; // Ändert Optionen, nicht nur Decodieren der Grenzen
    opts.inSampleSize = scale > 1 ? scale : 1; // Ändert Optionen, Skalierungsprozentsatz beim Laden des Bildes
    return BitmapFactory.decodeStream(bais, null, opts); // Laden des Bildes (Komprimiertes Bild erhalten)
  }
  /**
   * Methode zur Komprimierung von Bildern mit Bildschirmauflösung und angegebener Klarheit
   *
   * @param context
   * @param path  Pfad des Bildes
   * @return
   */
  public static Bitmap comp(Context context, String path) {
    return compressImage(getUsableImage(context, path));
  }
  /**
   * Bitmap mit Bildschirmauflösung erhalten
   *
   * @param context
   * @param path  Pfad des Bildes
   * @return
   */
  public static Bitmap getUsableImage(Context context, String path) {
    BitmapFactory.Options opts = new BitmapFactory.Options(); // Optionenobjekt (wird beim Laden des Bildes verwendet)
    opts.inJustDecodeBounds = true; // Optionen ändern, nur Größe erhalten
    BitmapFactory.decodeFile(path, opts); // Laden des Bildes (nur Bildgröße erhalten)
    DisplayMetrics metrics = new DisplayMetrics();
    metrics = context.getApplicationContext().getResources().getDisplayMetrics();
    int SkalierungX = opts.outWidth / metrics.widthPixels; // X-Achsenverhältnis der Skalierung (Bildbreite/Bildschirmbreite)
    int SkalierungY = opts.outHeight / metrics.heightPixels; // Y-Achsenverhältnis der Skalierung
    int scale = SkalierungX > SkalierungY &63; SkalierungX : SkalierungY; // Verhältnis der Bildskalierung (welches von X und Y auswählen, was zuerst gewählt wird)
    opts.inJustDecodeBounds = false; // Ändert Optionen, nicht nur Decodieren der Grenzen
    opts.inSampleSize = scale > 1 ? scale : 1; // Ändert Optionen, Skalierungsprozentsatz beim Laden des Bildes
    return BitmapFactory.decodeFile(path, opts); // Lädt das Bild (erhält das skalierte Bild)
  }
  /**
   * Komprimiert die Klarheit des Bildes auf die angegebene Größe
   *
   * @param image
   * @return
   */
  public static Bitmap compressImage(Bitmap image) {
    int maxLength = 1024 * 1024; // (byte)
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// Qualitätskomprimierungsmodus, hier100 bedeutet, nicht zu komprimieren, speichert die komprimierten Daten in baos
    int options = 100;
    while (baos.toByteArray().length > maxLength) { // Schleife: Überprüfen, ob das Bild nach der Komprimierung größer ist1mb, weiter komprimieren, wenn größer
      options -= 10;// Jedes Mal wird reduziert10
      baos.reset();// Setzt baos zurück, d.h. leert baos
      image.compress(Bitmap.CompressFormat.JPEG, options, baos);// Hier wird komprimiert options%, die komprimierten Daten werden in baos gespeichert
    }
    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// Speichert die komprimierten Daten baos in ByteArrayInputStream
    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// Erzeugt ein Bild aus den Daten von ByteArrayInputStream
    return bitmap;
  }
  /**
   * Bestimmt den Bildkomprimierungsmodus für Auflösung und Klarheit
   *
   * @param fromFile
   * @param toFile
   * @param reqWidth
   * @param reqHeight
   * @param quality
   */
  public static void transImage(String fromFile, String toFile, int reqWidth, int reqHeight, int quality) {
    Bitmap bitmap = BitmapFactory.decodeFile(fromFile);
    int bitmapWidth = bitmap.getWidth();
    int bitmapHeight = bitmap.getHeight();
    // Skaliergröße
    float scaleWidth = (float) reqWidth / bitmapWidth;
    float scaleHeight = (float) reqHeight / bitmapHeight;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    // Erstellen eines Bitmap-Objekts nach Skalierung
    Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, false);
    // in Datei speichern
    bitmap2File(toFile, quality, resizeBitmap);
    if (!bitmap.isRecycled()) {
      // Ressourcen freigeben, um OOM zu verhindern
      bitmap.recycle();
    }
    if (!resizeBitmap.isRecycled()) {
      resizeBitmap.recycle();
    }
  }
  /**
   * Bitmap in File umwandeln
   *
   * @param toFile
   * @param quality
   * @param bitmap
   * @return
   */
  public static File bitmap2File(String toFile, int quality, Bitmap bitmap) {
    File captureFile = new File(toFile);
    FileOutputStream out = null;
    try {
      out = new FileOutputStream(captureFile);
      if (bitmap.compress(Bitmap.CompressFormat.JPEG, quality, out)) {
        out.flush();
        out.close();
      }
    catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return captureFile;
  }
  /**
   * Drawable转换为Bitmap
   *
   * @param drawable
   * @return
   */
  public static Bitmap drawableToBitamp(Drawable drawable) {
    int w = drawable.getIntrinsicWidth();
    int h = drawable.getIntrinsicHeight();
    Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE &63; Bitmap.Config.ARGB_8888
        : Bitmap.Config.RGB_565;
    Bitmap bitmap = Bitmap.createBitmap(w, h, config);
    // 注意,下面三行代码要用到,否在在View或者surfaceview里的canvas.drawBitmap会看不到图
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, w, h);
    drawable.draw(canvas);
    return bitmap;
  }
  // Bitmap、Drawable、InputStream、byte[] 之间转换
  /**********************************************************/
  // 1. Bitmap to InputStream
  public static InputStream bitmap2Input(Bitmap bitmap, int quality) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, quality, baos);
    return new ByteArrayInputStream(baos.toByteArray());
  }
  public static InputStream bitmap2Input(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return new ByteArrayInputStream(baos.toByteArray());
  }
  // 2. Bitmap to byte[]
  public static byte[] bitmap2ByteArray(Bitmap bitmap, int quality) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, quality, baos);
    return baos.toByteArray();
  }
  public static byte[] bitmap2ByteArray(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
  }
  // 3. Drawable to byte[]
  public static byte[] drawable2ByteArray(Drawable drawable) {
    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
    return out.toByteArray();
  }
  // 4. byte[] to Bitmap
  public static Bitmap byteArray2Bitmap(byte[] bytes) {
    return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
  }
}

Dies ist der Abschluss dieses Artikels, ich hoffe, er hilft Ihnen bei Ihrem Lernen und ich hoffe, dass Sie die呐喊教程大力支持。

Erklärung: Der Inhalt dieses Artikels wurde aus dem Internet bezogen und gehört dem Urheberrechtsinhaber. Der Inhalt wurde von Internetnutzern selbstständig beigesteuert und hochgeladen, die Website besitzt keine Eigentumsrechte, hat die Inhalte nicht manuell bearbeitet und übernimmt keine rechtlichen Haftung. Wenn Sie Inhalte finden, die möglicherweise gegen das Urheberrecht verstoßen, sind Sie herzlich eingeladen, eine E-Mail an notice#w zu senden:3Erklärung: Bitte ersetzen Sie bei E-Mails das # durch @ und melden Sie Missbrauch, und fügen Sie relevante Beweise bei. Sobald nachgewiesen, wird die Website den涉嫌侵权的内 容立即删除。

Gefällt dir