Question

I want to create an cropping application using surfaceview on the android operating system. I've made ​​a display surface for displaying direct view camera, but I still fails when I want to add a cropping feature directly on the camera display. Figure edits below this is going to accomplish:

enter image description here

When the capture button is clicked then the images result will appear on the screen, as shown below:

enter image description here

The following is the source code MainActivity.java

package com.example.CameraPreview1;

import java.io.IOException;
import java.util.List;

import curso.citic15.camara.R;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends Activity implements SurfaceHolder.Callback, Camera.ShutterCallback, Camera.PictureCallback {
    private SurfaceView Surface;
    Camera camera;
    Bitmap InputImages;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Surface = (SurfaceView) findViewById(R.id.surface);
        Surface.getHolder().addCallback(this);

        ImageButton shutter = (ImageButton) findViewById(R.id.button_capture);
        shutter.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                camera.takePicture(MainActivity.this, null, null, MainActivity.this);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        camera.release();
    }

    @Override
    protected void onPause() {
        super.onPause();
        camera.stopPreview();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onShutter() {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if (camera != null) {
            Camera.Parameters params = camera.getParameters();
            List<Camera.Size> sizes = params.getSupportedPreviewSizes();
            Camera.Size selected = sizes.get(0);
            params.setPreviewSize(selected.width, selected.height);
            camera.setParameters(params);
            camera.setDisplayOrientation(90);


            try {
                camera.setPreviewDisplay(holder);
            } catch (IOException e) {
                e.printStackTrace();
            }

            camera.startPreview();
        } else {
            Toast.makeText(this, "No hay camara o hay algœn error.", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        camera = Camera.open();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.d("", "Destroyed");
    }

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 6;

        InputImages = BitmapFactory.decodeByteArray(data, 0, data.length, options);

        camera.startPreview();
    }

}

and below is the source code main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<SurfaceView
    android:id="@+id/surface"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

<ImageButton
    android:id="@+id/button_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="15dp"
    android:layout_marginRight="20dp"
    android:src="@drawable/dua"
    android:background="@null"
     />

<ImageButton
    android:id="@+id/button_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="15dp"
    android:layout_marginLeft="20dp"
    android:src="@drawable/satu"
    android:background="@null"
     />

<ImageButton
    android:id="@+id/button_capture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="10dp"
    android:src="@drawable/capture"
    android:background="@null"
     />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:src="@null" />
</RelativeLayout>

I've been looking on Google but none of the tutorials that provide the things I wanted, I had a bit of stress and despair. Will you help solve the problems mentioned above??? I would be very happy if that helps. Thank you very much

Was it helpful?

Solution

Check this tutorial. Displays a camera preview and a custom view than can define your region to be cropped, by dragging. You can modify it according to your needs. The only problems I could find are with orientation of the screen, that modify the preview, and of the resulting image.

http://adblogcat.com/a-camera-preview-with-a-bounding-box-like-google-goggles/

OTHER TIPS

you need to save the bitmap picture before crop.

croppedBitmap = Bitmap.createBitmap(cameraBitmap, x,y, cameraBitmap.getWidth(), cameraBitmap.getHeight());

after you just overwrite to the same path of your original picture

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top