Ağu 28

Android Fotoğraf Çekme ve ImageView’da Gösterme

Merhaba arkadaşlar fotoğraf çekme ile bir süredir uğraşmaktayım. Bu uğraşlarım sonucu elde ettiğim bilgi birikimimi sizlerle paylaşacağım. Öncelikle fotoğraf çekmekten başlayalım. layout’umuza bir buton ve bir imageview yerleştirelim.

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/fotoekleimageview"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Fotoğraf Çek"
    android:id="@+id/fotoeklefotocek"
    android:layout_below="@+id/fotoekleimageview"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="86dp" />

Şimdi Java kısmında bunları tanımlayalım ve fotoğraf çekme kodlarına geçelim.

ImageView fotogoster;
Button fotocek;
private static String imageFolderPath = null;
private static String root = null;
private String imageName = null;
private static Uri fileUri = null;
String path="";
boolean dirExists = true;
private static final int CAMERA_IMAGE_REQUEST=1;
private static int RESULT_LOAD_IMG = 1;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    fotogoster=(ImageView)findViewById(R.id.fotoekleimageview);
    fotocek=(Button)findViewById(R.id.fotoeklefotocek);
    fotocek.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        captureImage(v);
        deg=true;
    }
    });
}
public void captureImage(View view) {

    // fetching the root directory
    root = Environment.getExternalStorageDirectory().toString() + "/Your_Folder";
    // Creating folders for Image
    imageFolderPath = root + "/saved_images";
    File imagesFolder = new File(imageFolderPath);
    imagesFolder.mkdirs();
    // Generating file name
    imageName = "test.png";
    // Creating image here
    File image = new File(imageFolderPath, imageName);
    fileUri = Uri.fromFile(image);
    path=image.getAbsolutePath();
    Bitmap b= BitmapFactory.decodeFile(path);
    //Bitmap out = Bitmap.createScaledBitmap(b, 5, 5, false);
    File file = new File(path, "test.png");
    FileOutputStream fOut;
    try {
        fOut = new FileOutputStream(file);
        //out.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
        b.recycle();
        //out.recycle();
    } catch (Exception e) {}
    fotogoster.setTag(imageFolderPath + File.separator + imageName);
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    startActivityForResult(takePictureIntent, CAMERA_IMAGE_REQUEST);

}
//Kamera- Resim Çek ve Kaydet
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case CAMERA_IMAGE_REQUEST:
                    Bitmap bitmap = null;
                    try {
                        GetImageThumbnail getImageThumbnail = new GetImageThumbnail();
                        bitmap = getImageThumbnail.getThumbnail(fileUri, this);
                        bitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }

                    fotogoster.setImageBitmap(bitmap);
                    break;
                default:
                    Toast.makeText(this, "Hata...", Toast.LENGTH_SHORT).show();
                    break;
            }

        }
    }

GetImageThumbnail.java

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

public class GetImageThumbnail {
   private static int getPowerOfTwoForSampleRatio(double ratio) {
       int k = Integer.highestOneBit((int) Math.floor(ratio));
       if (k == 0)
           return 1;
       else
           return k;
   }

   public Bitmap getThumbnail(Uri uri, Context context)
           throws FileNotFoundException, IOException {
       InputStream input = context.getContentResolver().openInputStream(uri);

       BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
       onlyBoundsOptions.inJustDecodeBounds = true;
       onlyBoundsOptions.inDither = true;// optional
       onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
       BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
       input.close();
       if ((onlyBoundsOptions.outWidth == -1)
               || (onlyBoundsOptions.outHeight == -1))
           return null;

       int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight
               : onlyBoundsOptions.outWidth;

       double ratio = (originalSize > 400) ? (originalSize / 350) : 1.0;

       BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
       bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
       bitmapOptions.inDither = true;// optional
       bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
       input = context.getContentResolver().openInputStream(uri);
       Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
       input.close();
       return bitmap;
   }
            
}

h

Bir Cevap Yazın