37

i need to capture image from required portion of the screen.

capture image from camera.

at that time other screen content as it is.

enter image description here

how is this possible?

Mr.Sandy
  • 4,299
  • 3
  • 31
  • 54
  • Are you using the camera intent or you app implement you own camera preview surface? – Derzu Mar 16 '13 at 16:44
  • You wanna show de preview on fullscreen and save only the region labeled "camera view"? Or you wanna that the preview shows only on the region labeled "camera view"? – Derzu Mar 16 '13 at 16:51
  • 5
    i wanna wanna that the preview shows only on the region labeled "camera view". and I'm used Camera Intent. – Mr.Sandy Mar 16 '13 at 17:38
  • Ok, so you need to create your own camera preview surface, you cannot do that using the default camera app intent. I have already done some similar code to another problem, I will try to write you an answer later, maybe tomorrow. – Derzu Mar 16 '13 at 18:22
  • Are you still needing this the solution? Or did you solve it. – Derzu Mar 19 '13 at 23:24
  • i got some solution, but not perfect so, if you have then share. – Mr.Sandy Mar 20 '13 at 05:24
  • for surface view we used to custom camera view. –  Apr 17 '13 at 13:36
  • can you share the solution? – jake oliver Jul 21 '18 at 07:27

6 Answers6

29

try to use Surface View for creating dynamic camera view and set in your required portion.

following code try

variables set Class level (Global)

Button btn_capture;
Camera camera1;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
public static boolean previewing = false;

Following code in onCreate() method

getWindow().setFormat(PixelFormat.UNKNOWN);
    surfaceView = new SurfaceView(this);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
btn_capture = (Button) findViewById(R.id.button1);

surfaceView.setBackgroundResource(R.drawable.your_background_image);

if(!previewing){

        camera1 = Camera.open();
        if (camera1 != null){
            try {
                camera1.setDisplayOrientation(90);
                camera1.setPreviewDisplay(surfaceHolder);
                camera1.startPreview();
                previewing = true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

btn_capture.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            if(camera != null)
            {
                camera1.takePicture(myShutterCallback, myPictureCallback_RAW, myPictureCallback_JPG);

            }
        }
    });

Following code put after onCreate() in your class.

ShutterCallback myShutterCallback = new ShutterCallback(){

    public void onShutter() {
        // TODO Auto-generated method stub
    }};

PictureCallback myPictureCallback_RAW = new PictureCallback(){

    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub
    }};

PictureCallback myPictureCallback_JPG = new PictureCallback(){

    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub
        Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);

        Bitmap correctBmp = Bitmap.createBitmap(bitmapPicture, 0, 0, bitmapPicture.getWidth(), bitmapPicture.getHeight(), null, true);

    }};

public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub
    if(previewing){
        camera1.stopPreview();
        previewing = false;
    }

    if (camera1 != null){
        try {
            camera1.setPreviewDisplay(surfaceHolder);
            camera1.startPreview();
            previewing = true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub

}

public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub

        camera1.stopPreview();
        camera1.release();
        camera1 = null;
        previewing = false;

}

in AndroidManifest.xml give user-permissions.

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>

and also not forgot ( implements SurfaceHolder.Callback ) to the class.

4

I already created like that kind of Camera. What I did is, I covered the other area of the camera with an image, and cut the center part of the image and save it as png file, to make the center transparent.

You will set background image of your frame(camera preview) with that image. So that it will look like the camera is only that portion that is transparent or the circle.

I used this tutorial to open,create preview,and take picture from camera device http://developer.android.com/guide/topics/media/camera.html

in this part(u can see this in the link I provide above)

 private PictureCallback mPicture = new PictureCallback() {

 @Override
 public void onPictureTaken(byte[] data, Camera camera) {
   //this is where you crop your image
    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inMutable = true;
    Bitmap bitmap = BitmapFactory
            .decodeByteArray(data, 0, data.length, opt);

    bitmap=Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas mcanvas=new Canvas(bitmap);
    //do the cropping here, bitmap is the image you will use to crop

  }

 }

follow this tutorial on how to crop the image to circle Cropping circular area from bitmap in Android

Community
  • 1
  • 1
She Smile GM
  • 1,322
  • 1
  • 11
  • 33
  • But this solutions wouldn't work if a face is to be detected and auto captured, as it could be out of the circular area – user2234 Oct 30 '17 at 01:21
1

If the part of the screen is actually a view, you can capture only this view. Like this:

Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),view.getHeight(),Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);

If you want to capture only small portion of a view, you have to calculate rectangle of this side. Then:

Bitmap bitmap = Bitmap.createBitmap(rect.width(),rect.height(),Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.save();
canvas.translate(-rect.left,-rect.top);
view.draw(canvas);
canvas.restore();

It's only a pseudocode, but I hope you get the idea. Simply translate and draw only the part you need.

Zielony
  • 16,239
  • 6
  • 34
  • 39
  • 5
    i think you know this @Zielony, but you can not perfectly understand my requirements. please told me how can i set camera in this portion only from my full screen ? and capture image from that. after that bitmap and canvas used to draw and store image for other use. – Mr.Sandy Mar 13 '13 at 17:05
  • If you only need a part of the picture captured from the camera, you can simply crop the image. Capture the picture: http://stackoverflow.com/questions/5991319/capture-image-from-camera-and-display-in-activity and then crop it using Bitmap.createBitmap with x,y,width,height parameters. – Zielony Mar 13 '13 at 17:09
  • 5
    no..no..i not need to crop image but i need when i open camera it is not open in full screen but only open in portion of the screen which is set for that. same as above image. – Mr.Sandy Mar 13 '13 at 17:13
1

I use the CameraPreview in ApiDemos application and edit it as your requirement.

First, copy the code of the Preview class into a new class file in the same package so that it is public and you can declare it in the xml layout file. Remember to add one more constructor as below:

public Preview(Context context, AttributeSet attrs) {
    super(context, attrs);
    mSurfaceView = new SurfaceView(context);
    addView(mSurfaceView);

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = mSurfaceView.getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

Sample layout file with sample width and height:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Abow"/>

   <com.example.android.apis.graphics.Preview
       android:id="@+id/camera_view"
       android:layout_width="240dp"
       android:layout_height="180dp">
   </com.example.android.apis.graphics.Preview>     

   <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Below"/>

</LinearLayout>

In the onCreate() method of the CameraPreview activity, change the setContentView part as followed:

setContentView(R.layout.camera_layout);
mPreview = (Preview) findViewById(R.id.camera_view);
Binh Tran
  • 2,478
  • 1
  • 21
  • 18
1

use TextureView for preview,set layout_width and layout_height what ever you want. here is the code:

public class MainActivity extends Activity implements TextureView.SurfaceTextureListener {
    private Camera mCamera;
    private TextureView mTextureView;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTextureView = (TextureView) findViewById(R.id.textureView);
        mTextureView.setSurfaceTextureListener(this);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i2) {
        mCamera = Camera.open();

        try {
            mCamera.setPreviewTexture(surfaceTexture);
            mCamera.setDisplayOrientation(90);
            mCamera.startPreview();
        } catch (IOException exception) {

        }
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i2) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
        mCamera.startPreview();
        mCamera.release();
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}

and xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextureView
        android:layout_gravity="center"
        android:id="@+id/textureView"
        android:layout_width="200dp"
        android:layout_height="300dp"/>
</LinearLayout>
kidfolk
  • 11
  • 2
  • 1
    Where it's worth noting that `TextureView` wasn't introduced until ICS (API level 14). – MH. May 25 '13 at 01:57