public class MainActivity extends Activity implements SurfaceHolder.Callback {
private Camera mCamera;
private Button fCekBtn,img_yukle;
private SurfaceView srfView;
private PackageManager pm;
static int sayac=0;
private Boolean hasCamera = false;
private static String TAG=MainActivity.class.getSimpleName();
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fCekBtn = (Button) findViewById(com.necatievren.camera.R.id.fCekBtn);
img_yukle= (Button) findViewById(R.id.img_yukle);
srfView = (SurfaceView) findViewById(com.necatievren.camera.R.id.surfaceView);
img=(ImageView)findViewById(R.id.imgPicker);
pm = this.getPackageManager();
if(pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)){
hasCamera = true;
SurfaceHolder holder = srfView.getHolder();
holder.addCallback(this);
// holder.setFormat(PixelFormat.TRANSPARENT);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.setFixedSize(1000, 400);
//srfView değerini xml içinde 1px-1px en ve yükseklik verdiler
}else{
Log.v(TAG, " Kamera yok");
}
//----------------------------------
fCekBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(hasCamera){
takePicture();
}else{
Log.v(TAG, " Kamera yok");
}
}
});
//----------------------------------
img_yukle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final File sd_path = Environment.getExternalStorageDirectory();///mnt/sdcard
final String sd_filePath = "/BeoReset/Backup/";
final String sd_dbfilePath = sd_path + sd_filePath;
loadImageFromStorage(sd_dbfilePath);
}
});
}
//----------------------------------------------------------------------------------------------
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {}
//----------------------------------------------------------------------------------------------
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera=onKamerayiAc();
// mCamera= Camera.open();
mCamera.setDisplayOrientation(0);
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) { e.printStackTrace(); }
}
//----------------------------------------------------------------------------------------------
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
mCamera.stopPreview();
mCamera.release();
}
//----------------------------------------------------------------------------------------------
private void takePicture(){
// mCamera=onKamerayiAc();
mCamera.takePicture(_shutterCallBack, _rawCallBack, _jpgCallBack);
}
//----------------------------------------------------------------------------------------------
ShutterCallback _shutterCallBack = new ShutterCallback(){
@Override
public void onShutter() {//implement method
}
};
//----------------------------------------------------------------------------------------------
PictureCallback _rawCallBack=new PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {//implement method
}
};
//----------------------------------------------------------------------------------------------
PictureCallback _jpgCallBack = new PictureCallback(){
@Override
public void onPictureTaken(byte[] data, Camera camera) {
final File sd_path= Environment.getExternalStorageDirectory();///mnt/sdcard
final String sd_filePath="/Beo/Backup/";
final String sd_dbfilePath =sd_path +sd_filePath;
File currentFile = new File(sd_dbfilePath);
if (!currentFile.exists()) {
currentFile.mkdirs();
FileOutputStream foutStream = null;
try {
foutStream = new FileOutputStream(sd_dbfilePath+"/beo_img"+ ++sayac+".jpg");
foutStream.write(data);
foutStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e("","Dosya Bulunamadı: "+e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.e("", "Erişim Hatası: " + e.toString());
}
}else{
FileOutputStream foutStream = null;
try {
foutStream = new FileOutputStream(sd_dbfilePath+"/beo_img"+ ++sayac+".jpg");
foutStream.write(data);
foutStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e("","Dosya Bulunamadı: "+e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.e("", "Erişim Hatası: " + e.toString());
}
}
}
};
//----------------------------------------------------------------------------------------------
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
private Camera onKamerayiAc() {
Log.d(TAG,"_adim_1");
int cameraCount = 0;
Camera cam = null;Log.d(TAG,"_adim_2");
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();Log.d(TAG,"_adim_3");
cameraCount = Camera.getNumberOfCameras();Log.d(TAG,"_adim_4---"+cameraCount);
for (int camIdx = 0; camIdx < cameraCount; camIdx++) {Log.d(TAG,"_adim_5");
Camera.getCameraInfo(camIdx, cameraInfo);Log.d(TAG, "_adim_6");
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
cam = Camera.open(camIdx);Log.d(TAG,"_adim_7");
} catch (RuntimeException e) {
Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
}
}
}
return cam;
}
//----------------------------------------------------------------------------------------------
private void loadImageFromStorage(String path)//tekrar sdcarttan yüklüyoruz
{
try {
File f=new File(path, "/nvs_reset"+ ++sayac+".jpg");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
img.setImageBitmap(b);
}
catch (FileNotFoundException e)
{e.printStackTrace();}
}
}