Last active
January 1, 2016 12:39
-
-
Save ishitcno1/8146367 to your computer and use it in GitHub Desktop.
Implement android camera.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="horizontal"> | |
<FrameLayout | |
android:id="@+id/main_fl_camera_preview" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_weight="1" /> | |
<Button | |
android:id="@+id/main_btn_capture" | |
android:layout_width="wrap_content" | |
android:layout_height="match_parent" | |
android:text="Capture" | |
android:onClick="onMainBtnCaptureClick"/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.edinstudio.app.mycamera" | |
android:versionCode="1" | |
android:versionName="1.0" | |
android:installLocation="preferExternal"> | |
<uses-feature android:name="android.hardware.camera" /> | |
<uses-permission android:name="android.permission.CAMERA" /> | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |
<uses-sdk | |
android:minSdkVersion="10" | |
android:targetSdkVersion="19" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme"> | |
<activity | |
android:name="com.edinstudio.app.mycamera.MainActivity" | |
android:label="@string/app_name" | |
android:screenOrientation="landscape"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.edinstudio.app.mycamera; | |
import android.content.Context; | |
import android.hardware.Camera; | |
import android.view.SurfaceHolder; | |
import android.view.SurfaceView; | |
import java.io.IOException; | |
/** | |
* Created by albert on 12/27/13. | |
*/ | |
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { | |
private SurfaceHolder mHolder; | |
private Camera mCamera; | |
public CameraPreview(Context context, Camera camera) { | |
super(context); | |
mCamera = camera; | |
mHolder = getHolder(); | |
mHolder.addCallback(this); | |
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); | |
} | |
@Override | |
public void surfaceCreated(SurfaceHolder surfaceHolder) { | |
try { | |
mCamera.setPreviewDisplay(surfaceHolder); | |
mCamera.startPreview(); | |
} catch (IOException e) { | |
} | |
} | |
@Override | |
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) { | |
} | |
@Override | |
public void surfaceDestroyed(SurfaceHolder surfaceHolder) { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.edinstudio.app.mycamera; | |
import android.app.Activity; | |
import android.hardware.Camera; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.FrameLayout; | |
public class MainActivity extends Activity { | |
public static final String TAG = "mycamera"; | |
private FrameLayout mFlCameraPreview; | |
private Camera mCamera; | |
private CameraPreview mCameraPreview; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mFlCameraPreview = (FrameLayout) findViewById(R.id.main_fl_camera_preview); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
if (mCamera == null) { | |
mCamera = getCameraInstance(); | |
} | |
if (mCameraPreview == null) { | |
mCameraPreview = new CameraPreview(this, mCamera); | |
mFlCameraPreview.addView(mCameraPreview); | |
} | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
if (mCamera != null) { | |
mCamera.stopPreview(); | |
mCamera.release(); | |
mCamera = null; | |
} | |
if (mCameraPreview != null) { | |
mFlCameraPreview.removeView(mCameraPreview); | |
mCameraPreview = null; | |
} | |
} | |
public static Camera getCameraInstance() { | |
Camera camera = null; | |
try { | |
camera = Camera.open(); | |
} catch (Exception e) { | |
} | |
return camera; | |
} | |
public void onMainBtnCaptureClick(View view) { | |
mCamera.takePicture(null, null, null, new MyPictureCallback()); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.edinstudio.app.mycamera; | |
import android.hardware.Camera; | |
import android.os.Environment; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
/** | |
* Created by albert on 12/29/13. | |
*/ | |
public class MyPictureCallback implements Camera.PictureCallback { | |
public static final int MEDIA_TYPE_IMAGE = 1; | |
public static final int MEDIA_TYPE_VIDEO = 2; | |
@Override | |
public void onPictureTaken(byte[] bytes, Camera camera) { | |
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE); | |
if (pictureFile == null) { | |
return; | |
} | |
try { | |
FileOutputStream fileOutputStream = new FileOutputStream(pictureFile); | |
fileOutputStream.write(bytes); | |
fileOutputStream.close(); | |
} catch (FileNotFoundException e) { | |
} catch (Exception e) { | |
} | |
camera.startPreview(); | |
} | |
private static File getOutputMediaFile(int type) { | |
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory( | |
Environment.DIRECTORY_PICTURES), "MyCamera"); | |
if (!mediaStorageDir.exists()) { | |
if (!mediaStorageDir.mkdirs()) { | |
return null; | |
} | |
} | |
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); | |
File mediaFile; | |
if (type == MEDIA_TYPE_IMAGE) { | |
mediaFile = new File(mediaStorageDir.getPath() + File.separator + | |
"IMG_" + timeStamp + ".jpg"); | |
} else if (type == MEDIA_TYPE_VIDEO) { | |
mediaFile = new File(mediaStorageDir.getPath() + File.separator + | |
"VID_" + timeStamp + ".mp4"); | |
} else { | |
return null; | |
} | |
return mediaFile; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment