Android自定义相机SurfaceView OnClick在SD卡上保存图像?

时间:2014-11-24 18:15:28

标签: android camera surfaceview surfaceholder

您好我想实现自定义Android相机应用程序,其中包括相机预览顶部的imageview。点击屏幕后,我想在SD卡中存储图像。到目前为止,我已经完成了这一部分。但我无法点击SurfaceView任何人都告诉我如何解决我的问题。到目前为止,它打开相机,它有cameraview顶部的相机。但是onclick方法不起作用。也有人可以告诉我如何在onclick函数调用时保存图像。这是我的代码。

这是主要布局

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

    <SurfaceView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/surfaceView"
            android:layout_gravity="center"/>
</LinearLayout>

这是自定义视图(activity_camera)

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

    <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/border"
            android:id="@+id/imageView"/>
</LinearLayout>

这是java代码。

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Toast;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: Sajith
 * Date: 11/18/14
 * Time: 3:41 PM
 * To change this template use File | Settings | File Templates.
 */
public class CameraPreview extends Activity implements SurfaceHolder.Callback,View.OnClickListener {
    Camera camera;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    boolean previewing = false;
    LayoutInflater controlInflater = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

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

        controlInflater = LayoutInflater.from(getBaseContext());
        View viewControl = controlInflater.inflate(R.layout.activity_camera, null);
        LayoutParams layoutParamsControl
                = new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT);
        this.addContentView(viewControl, layoutParamsControl);

    }



    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {

        if(previewing){
            camera.stopPreview();
            previewing = false;
        }

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

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

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
    }

    @Override
    public void onClick(View view) {
        Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_LONG).show();
    }
}

由于

1 个答案:

答案 0 :(得分:1)

capture.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        camera.takePicture(null,null, jpegCallback);
    }
});

final Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        FileOutputStream outStream = null;
        try {
            outStream = new FileOutputStream(String.format("/sdcard/YourImage.jpg")); 
            outStream.write(data);
            outStream.close();
        } catch (FileNotFoundException e) { 
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        camera.startPreview();
    }
};