Jak przechwycić obraz z niestandardowego CameraView w Androidzie?

I need to capture image od wymagana część Ekranu .

Przechwytywanie Obrazu z Kamery .

W tym czasie inna zawartość ekranu jaka jest .

Tutaj wpisz opis obrazka

Jak to jest możliwe ?

Author: Mr.Sandy, 2013-03-13

6 answers

Spróbuj użyć Surface View do tworzenia dynamicznych kamera Wyświetl i ustaw w wymaganej porcji.

Following code try

Variables set Class level (Global)

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

Następujący kod w metodzie OnCreate ()

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);

            }
        }
    });

Następujący kod umieść po onCreate () W twojej klasie.

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;

}

W AndroidManifest.XML daje uprawnienia użytkownika.

<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"/>

A także nie zapomniałem ( implementuje SurfaceHolder.Callback) do klasy.

 26
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-22 13:53:24

Już stworzyłem taki aparat. Zrobiłem to, pokryłem drugi obszar aparatu obrazem, wyciąłem środkową część obrazu i zapisałem go jako plik png, aby środek był przezroczysty.

Za pomocą tego obrazu ustawisz obraz tła swojej klatki(podgląd kamery). Tak, że będzie wyglądać jak aparat jest tylko ta część, która jest przezroczysta lub okrąg.

Użyłem tego samouczka do otwierania, tworzenia podglądu i robienia zdjęć z aparatu urządzenie http://developer.android.com/guide/topics/media/camera.html

W tej części (można to zobaczyć w linku podaję powyżej)

 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

  }

 }

Postępuj zgodnie z tym samouczkiem, jak przyciąć obraz do okręgu kadrowanie okrągłego obszaru z bitmapy w Androidzie

 3
Author: She Smile GM,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:17:42
 2
Author: Digvijay Aghera,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-13 17:40:15

Jeśli część ekranu jest w rzeczywistości widokiem, możesz przechwycić tylko ten widok. TAK:

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

Jeśli chcesz uchwycić tylko małą część widoku, musisz obliczyć prostokąt tej strony. Wtedy:

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();
To tylko pseudokod, ale mam nadzieję, że rozumiesz. Po prostu Przetłumacz i narysuj tylko część, której potrzebujesz.
 1
Author: Zielony,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-13 16:51:16

Używam CameraPreview w aplikacji ApiDemos i edytuję go jako twoje wymaganie.

Najpierw skopiuj kod klasy Preview do nowego pliku klasy w tym samym pakiecie, aby był publiczny i można go zadeklarować w pliku układu XML. Pamiętaj, aby dodać jeszcze jeden konstruktor jak poniżej:

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);
}

Przykładowy plik układu z przykładową szerokością i wysokością:

<?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>

W metodzie OnCreate () działania CameraPreview Zmień część setContentView w następujący sposób:

setContentView(R.layout.camera_layout);
mPreview = (Preview) findViewById(R.id.camera_view);
 1
Author: Binh Tran,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-18 10:29:53

Użyj TextureView do podglądu, Ustaw layout_width i layout_height, co chcesz. oto kod:

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.
    }
}

I plik xml:

<?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>
 1
Author: kidfolk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-19 15:29:31