22 Kasım 2017 Çarşamba

Bitmap Sınıfı

Şu satırı dahil ederiz.
import android.graphics.Bitmap;
Android Bitmap sınıfı JDK'dakinden biraz farklı özellikler içerir.

CompressFormat
PNG, JPEG enumları tanımlı.

createBitmap metodu - source + x+ y + width + height
Kaynak bitmap'i keserek yeni bir bitmap verir. Şöyle yaparız.
Bitmap image = Bitmap.createBitmap (image, 0, 0, 78, 78);
Kaynak bitmap ile aynı özelliklere sahip boş bir bitmap verir.
Bitmap source =... int newHeight = ...; int newWidth = ...;
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
Kaynak bir ekran bileşeni ise şöyle yaparız.
View v1 = ...;
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
Boş bitmap yaratır.
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
createBitmap metodu - width + height + config
Mutable Bitmap döndürür. Şöyle yaparız.
Bitmap source = ...;
Bitmap bitmap = Bitmap.createBitmap(src.getWidth(),src.getHeight(),
  src.getConfig());
createBitmap metodu - width + height + Matrix
Immutable Bitmap döndürür. Döndürerek yeni bir bitmap yaratır. Şöyle yaparız.
public static Bitmap rotate(Bitmap source, float angle) {
  Matrix matrix = new Matrix();
  matrix.postRotate(angle);
  return Bitmap.createBitmap(source, 0, 0, source.getWidth(),
                source.getHeight(), matrix, false);
}
Ölçeklendirerek yeni bir bitmap yaratır. Şöyle yaparız
Bitmap bitmap = ...;
int width = bitmap.getWidth();
int height = bitmap.getHeight();

float scaleWidth = ...;
float scaleHeight = ...;

Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);

Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,
            height, matrix, true);
createScaledBitmap metodu
İmzası şöyle. Kaynak bitmap'i ölçeklendirerek yeni bir bitmap verir.
public static Bitmap createScaledBitmap
  (Bitmap src, int dstWidth, int dstHeight, boolean filter)
Filter alanı genellikle true olarak kullanılır. Bitmap ölçeklenirken interpolation ile pixeller doldurulur. Şöyle yaparız.
Bitmap image = ...;
int width    = ...;
int height   = ...;

Bitmap scaledBitmap = Bitmap.createScaledBitmap(image, width, height, true);
Bitmap'i ölçeklendirerek tam ortasından kesme kodu burada.

compress metodu
Bitmap'i sıkıştırarak bir stream'e yazar.
Örnek
Şöyle yaparız.
Bitmap bitmap = ...
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
Örnek
Şöyle yaparız.
Bitmap bitmap =...;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
Örnek
Stream'i byte dizisine çevirmek için şöyle yaparız.
public byte[] convertBitmapToByteArray(Bitmap bitmap) {
  ByteArrayOutputStream stream = null;
  try {
    stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

    return stream.toByteArray();
  }finally {
    if (stream != null) {
      try {
        stream.close();
      } catch (IOException e) {
        ...
      }
    }
  }
}
Base64 olarak encode etmek için şöyle yaparız.
//For encoding toString
public String getStringImage(Bitmap bmp){
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
  byte[] imageBytes = baos.toByteArray();
  String encodedImage = android.util.Base64.encodeToString(imageBytes, Base64.DEFAULT);
  return encodedImage;
}
Base64 string'den tekrar bitmap'e dönmek için şöyle yaparız.
//For decoding
String str=encodedImage;
byte data[]= android.util.Base64.decode(str, android.util.Base64.DEFAULT);
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
getPixels metodu
Şöyle yaparız.
Bitmap bitmap = ...;

int w = bitmap.getWidth();
int h = bitmap.getHeight();

int[] pix = new int[w * h];
bitmap.getPixels(pix, 0, w, 0, 0, w, h);
getWidth ve getHeight
Şöyle yaparız.
Bitmap image = ...;
int width = image.getWidth();
int height = image.getHeight();



Hiç yorum yok:

Yorum Gönder