Advertisement
Guest User

Untitled

a guest
Mar 1st, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. public class MainActivity extends ActionBarActivity {
  2. public FrameLayout preview ;
  3. public Button Shutter_btn;
  4. private Camera mCamera;
  5. CameraPreview mPreview;
  6.  
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11.  
  12. // Create an instance of Camera
  13. mCamera = getCameraInstance();
  14. // Create our Preview view and set it as the content of our activity.
  15. mPreview = new CameraPreview(this, mCamera);
  16. FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
  17. preview.addView(mPreview);
  18.  
  19. Shutter_btn = (Button) findViewById(R.id.button3);
  20. Shutter_btn.setOnClickListener(new View.OnClickListener() {
  21.  
  22. @Override
  23. public void onClick(View v) {
  24. // TODO Auto-generated method stub
  25. TakePhoto();
  26. }
  27. });
  28. }
  29.  
  30. public void TakePhoto()
  31. {
  32.  
  33. PictureCallback mPicture = new PictureCallback() {
  34.  
  35. @Override
  36. public void onPictureTaken(byte[] data, Camera camera) {
  37.  
  38. File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
  39. if (pictureFile == null){
  40.  
  41. return;
  42. }
  43.  
  44. try {
  45. FileOutputStream fos = new FileOutputStream(pictureFile);
  46. fos.write(data);
  47. fos.close();
  48. } catch (FileNotFoundException e) {
  49. Log.d("TAG", "File not found: " + e.getMessage());
  50. } catch (IOException e) {
  51. Log.d("TAG", "Error accessing file: " + e.getMessage());
  52. }
  53. }
  54. };
  55. mCamera.takePicture(null, null, mPicture);
  56.  
  57. }
  58.  
  59. public void CameraRelease()
  60. {
  61. if(mCamera!=null){
  62. mCamera.stopPreview();
  63. mCamera.setPreviewCallback(null);
  64.  
  65. mCamera.release();
  66. mCamera = null;
  67. }
  68.  
  69. }
  70.  
  71. private void galleryAddPic(String ImagePath) {
  72. Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
  73. File f = new File(ImagePath);
  74. Uri contentUri = Uri.fromFile(f);
  75. mediaScanIntent.setData(contentUri);
  76. this.sendBroadcast(mediaScanIntent);
  77. }
  78. public final int MEDIA_TYPE_IMAGE = 1;
  79.  
  80. /** Create a file Uri for saving an image or video */
  81. private Uri getOutputMediaFileUri(int type){
  82. return Uri.fromFile(getOutputMediaFile(type));
  83. }
  84.  
  85. /** Create a File for saving an image or video */
  86. private File getOutputMediaFile(int type){
  87. // To be safe, you should check that the SDCard is mounted
  88. // using Environment.getExternalStorageState() before doing this.
  89.  
  90. File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp");
  91. // This location works best if you want the created images to be shared
  92. // between applications and persist after your app has been uninstalled.
  93.  
  94. // Create the storage directory if it does not exist
  95. if (! mediaStorageDir.exists()){
  96. if (! mediaStorageDir.mkdirs()){
  97. Log.d("MyCameraApp", "failed to create directory");
  98. Toast.makeText(MainActivity.this, "Dirrectory not been made " +mediaStorageDir , Toast.LENGTH_LONG).show();
  99. return null;
  100. }
  101. }
  102. Toast.makeText(MainActivity.this, "Dirrectory not been made " +mediaStorageDir , Toast.LENGTH_LONG).show();
  103. // Create a media file name
  104. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  105. File mediaFile;
  106. if (type == MEDIA_TYPE_IMAGE){
  107. mediaFile = new File(mediaStorageDir.getPath() + File.separator +
  108. "IMG_"+ timeStamp + ".jpg");
  109. }
  110. else {
  111. return null;
  112. }
  113.  
  114. return mediaFile;
  115. }
  116.  
  117.  
  118. /** A safe way to get an instance of the Camera object. */
  119. public Camera getCameraInstance(){
  120. Camera c = null;
  121. try {
  122. c = Camera.open(); // attempt to get a Camera instance
  123. }
  124. catch (Exception e){
  125. Toast.makeText(MainActivity.this, "No Camera", Toast.LENGTH_SHORT).show();// no camera on this device
  126. // Camera is not available (in use or does not exist)
  127. }
  128. return c; // returns null if camera is unavailable
  129. }
  130.  
  131.  
  132. }
  133.  
  134. package ahmed.Labib.mycamera;
  135.  
  136. import java.io.IOException;
  137.  
  138. import android.content.Context;
  139. import android.hardware.Camera;
  140. import android.util.Log;
  141. import android.view.SurfaceHolder;
  142. import android.view.SurfaceView;
  143.  
  144. /** A basic Camera preview class */
  145. public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
  146. private SurfaceHolder mHolder;
  147. public Camera mCamera;
  148.  
  149. public CameraPreview(Context context, Camera camera) {
  150. super(context);
  151. mCamera = camera;
  152.  
  153. // Install a SurfaceHolder.Callback so we get notified when the
  154. // underlying surface is created and destroyed.
  155. mHolder = getHolder();
  156. mHolder.addCallback(this);
  157. // deprecated setting, but required on Android versions prior to 3.0
  158. mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  159. }
  160.  
  161. public void surfaceCreated(SurfaceHolder holder) {
  162. // The Surface has been created, now tell the camera where to draw the preview.
  163. try {
  164. mCamera.setPreviewDisplay(holder);
  165. mCamera.startPreview();
  166. } catch (IOException e) {
  167. Log.d("TAG", "Error setting camera preview: " + e.getMessage());
  168. }
  169. }
  170.  
  171. public void surfaceDestroyed(SurfaceHolder holder) {
  172. // empty. Take care of releasing the Camera preview in your activity.
  173. }
  174.  
  175. public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
  176. // If your preview can change or rotate, take care of those events here.
  177. // Make sure to stop the preview before resizing or reformatting it.
  178.  
  179. if (mHolder.getSurface() == null){
  180. // preview surface does not exist
  181. return;
  182. }
  183.  
  184. // stop preview before making changes
  185. try {
  186. mCamera.stopPreview();
  187. } catch (Exception e){
  188. // ignore: tried to stop a non-existent preview
  189. }
  190.  
  191. // set preview size and make any resize, rotate or
  192. // reformatting changes here
  193.  
  194. // start preview with new settings
  195. try {
  196. mCamera.setPreviewDisplay(mHolder);
  197. mCamera.startPreview();
  198.  
  199. } catch (Exception e){
  200. Log.d("TAG", "Error starting camera preview: " + e.getMessage());
  201. }
  202. }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement