Advertisement
PialKanti

ImageCapture

Jan 29th, 2016
1,766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. package com.example.pial_pc.instantbookreview;
  2.  
  3. import android.content.pm.PackageManager;
  4. import android.graphics.Bitmap;
  5. import android.hardware.Camera;
  6. import android.os.Bundle;
  7. import android.os.Environment;
  8. import android.support.v7.app.ActionBarActivity;
  9. import android.util.Log;
  10. import android.view.SurfaceHolder;
  11. import android.view.SurfaceView;
  12. import android.view.View;
  13. import android.widget.ImageButton;
  14. import android.widget.Toast;
  15.  
  16. import com.example.pial_pc.instantbookreview.bitmapProcess.Tools;
  17. import com.example.pial_pc.instantbookreview.imageCapture.FocusBoxView;
  18.  
  19. import org.opencv.android.BaseLoaderCallback;
  20. import org.opencv.android.LoaderCallbackInterface;
  21. import org.opencv.android.OpenCVLoader;
  22. import org.opencv.android.Utils;
  23. import org.opencv.core.Mat;
  24. import org.opencv.core.Size;
  25. import org.opencv.imgproc.Imgproc;
  26.  
  27. import java.io.File;
  28. import java.io.FileOutputStream;
  29. import java.text.SimpleDateFormat;
  30. import java.util.Date;
  31.  
  32. /**
  33. * Created by Pial-PC on 1/19/2016.
  34. */
  35. public class ImageCapture extends ActionBarActivity implements SurfaceHolder.Callback {
  36. SurfaceView camera_frame;
  37. ImageButton shutter, focus;
  38. Camera.PictureCallback rawPictureCallback, jpegPictureCallback;
  39. Camera.ShutterCallback shutterCallback;
  40. Camera camera;
  41. FocusBoxView focusBox;
  42. Mat imageMat;
  43. Bitmap bmp;
  44.  
  45.  
  46. @Override
  47. protected void onCreate(Bundle savedInstanceState) {
  48. super.onCreate(savedInstanceState);
  49. setContentView(R.layout.activity_image_capture);
  50. getSupportActionBar().hide();
  51.  
  52. camera_frame = (SurfaceView) findViewById(R.id.camera_frame);
  53. shutter = (ImageButton) findViewById(R.id.shutter_button);
  54. focus = (ImageButton) findViewById(R.id.focus_button);
  55. focusBox=(FocusBoxView)findViewById(R.id.focus_box);
  56. SurfaceHolder surfaceHolder = camera_frame.getHolder();
  57. surfaceHolder.addCallback(this);
  58. surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  59.  
  60. shutter.setOnClickListener(new View.OnClickListener() {
  61. @Override
  62. public void onClick(View view) {
  63. //This code is for mute Camera shutter sound
  64.  
  65. Camera.CameraInfo info = new Camera.CameraInfo();
  66. for (int id = 0; id < Camera.getNumberOfCameras(); id++) {
  67. Camera.getCameraInfo(id, info);
  68. if (info.canDisableShutterSound) {
  69. camera.enableShutterSound(false);
  70. }
  71. }
  72.  
  73. camera.takePicture(shutterCallback, rawPictureCallback, jpegPictureCallback);
  74. }
  75. });
  76.  
  77. focus.setOnClickListener(new View.OnClickListener() {
  78. @Override
  79. public void onClick(View view) {
  80. Camera.AutoFocusCallback autoFocusCallback=new Camera.AutoFocusCallback() {
  81. @Override
  82. public void onAutoFocus(boolean b, Camera camera) {
  83.  
  84. }
  85. };
  86.  
  87. camera.autoFocus(autoFocusCallback);
  88. }
  89. });
  90.  
  91. jpegPictureCallback = new Camera.PictureCallback() {
  92. @Override
  93. public void onPictureTaken(byte[] bytes, Camera camera) {
  94.  
  95. if (bytes == null) {
  96. Log.d("OCR", "Got null data");
  97. return;
  98. }
  99.  
  100.  
  101. bmp= Tools.getFocusedBitmap(getApplicationContext(), camera, bytes, focusBox.getBox());
  102. bmp.setDensity(300);
  103.  
  104. //This below code is for image processing using openCV.
  105. imageMat=new Mat();
  106. Utils.bitmapToMat(bmp, imageMat);
  107. Imgproc.cvtColor(imageMat, imageMat, Imgproc.COLOR_BGR2GRAY);
  108. Imgproc.GaussianBlur(imageMat, imageMat, new Size(3, 3), 0);
  109. Imgproc.adaptiveThreshold(imageMat, imageMat, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 5, 4);
  110.  
  111. //Saving Bitmap to sdcard
  112. File checkingFileDir=new File(Environment.getExternalStorageDirectory()+"Inastant_Book_Reviwer/");
  113. if(!checkingFileDir.exists()){
  114. checkingFileDir.mkdir();
  115. }
  116. String timeStamp = new SimpleDateFormat("ddMMyyyy").format(new Date());
  117. File imageFile;
  118. String mImageName="Image_"+ timeStamp +".jpg";
  119. imageFile = new File(checkingFileDir.getPath() + File.separator + mImageName);
  120.  
  121. try{
  122. FileOutputStream outImage=new FileOutputStream(imageFile);
  123. bmp.compress(Bitmap.CompressFormat.PNG,90,outImage);
  124. outImage.close();
  125. }catch (Exception e){
  126. e.printStackTrace();
  127. }
  128.  
  129.  
  130. OCR_Image ocr_image=new OCR_Image();
  131. ocr_image.execute(ImageCapture.this,bmp);
  132.  
  133. Log.d("OCR", "Got bitmap");
  134.  
  135.  
  136. }
  137. };
  138.  
  139. shutterCallback = new Camera.ShutterCallback() {
  140. @Override
  141. public void onShutter() {
  142.  
  143. }
  144. };
  145.  
  146. }
  147.  
  148. @Override
  149. public void surfaceCreated(SurfaceHolder surfaceHolder) {
  150. try {
  151. //This checks if a device has a camera or not
  152. if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
  153. // this device has a camera
  154. camera = Camera.open();
  155. } else {
  156. // no camera on this device
  157. Toast.makeText(getApplicationContext(), "Your device doesn't have a Camera.", Toast.LENGTH_SHORT).show();
  158. }
  159. camera.setPreviewDisplay(surfaceHolder);
  160. camera.setDisplayOrientation(90);
  161. camera.startPreview();
  162. } catch (Exception e) {
  163. e.printStackTrace();
  164. }
  165. }
  166.  
  167. @Override
  168. public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
  169.  
  170. }
  171.  
  172. @Override
  173. public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
  174.  
  175. }
  176.  
  177. @Override
  178. public void onPause() {
  179. super.onPause();
  180.  
  181. if (camera != null) {
  182. camera.release();
  183. }
  184.  
  185. SurfaceHolder surfaceHolder = camera_frame.getHolder();
  186. surfaceHolder.removeCallback(this);
  187.  
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement