Today I had faced an issue in android surfaceview for camera customization
.
I tried the below code.
The Issue occurred when I captured the image, it stops the camera preview and doesn't return to the activity.
Following code will be implemented in the program. I took this code from an existing reference on stackoverflow
Supporting Class.
public class AndroidCameraSurfaceview extends Activity implements SurfaceHolder.Callback { TextView testView; Camera camera; SurfaceView surfaceView; SurfaceHolder surfaceHolder; boolean preview; PictureCallback rawCallback; ShutterCallback shutterCallback; PictureCallback jpegCallback; int displayheight, displaywidth; Camera.PreviewCallback previewCallback; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.camerasurfaceview); getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); surfaceView = (SurfaceView) findViewById(R.id.surfaceView); surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); jpegCallback = new PictureCallback() { public void onPictureTaken(byte[] data, Camera camera) { Bundle b = new Bundle(); b.putByteArray("Image", data); Intent intent = new Intent(); intent.putExtras(b); setResult(RESULT_OK, intent); finish(); // refreshCamera(); } }; } public void captureImage(View v) throws IOException { // take the picture camera.takePicture(null, null, jpegCallback); } public void refreshCamera() { if (surfaceHolder.getSurface() == null) { // preview surface does not exist return; } try { camera.stopPreview(); } catch (Exception e) { } try { camera.setDisplayOrientation(90); camera.setPreviewDisplay(surfaceHolder); camera.startPreview(); } catch (Exception e) { } } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if (preview) { camera.stopPreview(); } try{ Camera.Parameters parameters = camera.getParameters(); List<Size> sizes = parameters.getSupportedPreviewSizes(); Size optimalSize = getOptimalPreviewSize(sizes, width, height); parameters.setPreviewSize(optimalSize.width, optimalSize.height); camera.setParameters(parameters); try { camera.setDisplayOrientation(90); camera.setPreviewDisplay(holder); camera.startPreview(); preview = true; } catch (IOException e) { e.printStackTrace(); } }catch(Exception e){ System.out.println("Surface Exception---=>"+e); } } public void surfaceCreated(SurfaceHolder holder) { camera = Camera.open(); if (camera != null) { Camera.Parameters params = camera.getParameters(); camera.setDisplayOrientation(90); camera.setParameters(params); } } public void surfaceDestroyed(SurfaceHolder holder) { // stop preview and release camera camera.stopPreview(); camera.release(); camera = null; } private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) { final double ASPECT_TOLERANCE = 1; double targetRatio = (double) w / h; if (sizes == null) return null; Size optimalSize = null; double minDiff = Double.MAX_VALUE; int targetHeight = h; for (Size size : sizes) { double ratio = (double) size.width / size.height; if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue; if (Math.abs(size.height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } if (optimalSize == null) { minDiff = Double.MAX_VALUE; for (Size size : sizes) { if (Math.abs(size.height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } } return optimalSize; } }
2.Implemented in Activity
public void captureImage() {
Intent intentDriver = new Intent(AddNewDevice_Activity.this, AndroidCameraSurfaceview.class); startActivityForResult(intentDriver, 0); // // Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // // Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // // intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // // // start the image capture Intent // startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); // Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // // fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // // intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // // // start the image capture Intent // startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 0) { System.out.println("Result Code: " + resultCode); if (resultCode == RESULT_OK && data != null) { Bundle bundle = data.getExtras(); byte[] test = bundle.getByteArray("Image"); Bitmap bpCamera = BitmapFactory.decodeByteArray(test, 0, test.length); Matrix matrix = new Matrix(); matrix.postRotate(90); bpCamera = Bitmap .createBitmap(bpCamera, 0, 0, bpCamera.getWidth(), bpCamera.getHeight(), matrix, true); imageView_camera.setImageBitmap(bpCamera); selectedImageStr = encodeTobase64(bpCamera); } } else { finish(); } }
onActivityResult()
is not called afterAndroidCameraSurfaceview.finish()
inonPictureTaken()
? Does your log show thatonPictureTaken()
was called? Maybe, your main activityonCreate()
is called after that? – Refer