【问题标题】:Android custom camera SurfaceView OnClick save image on sdcard?Android自定义相机SurfaceView OnClick在sdcard上保存图像?
【发布时间】:2014-11-24 18:15:27
【问题描述】:

嗨,我想实现自定义 android 相机应用程序,其中包括在相机预览顶部的 imageview。单击屏幕后,我想将图像存储在 sdcard 中。到目前为止,我已经完成了这部分。但我无法单击 SurfaceView并保存图像。任何人都可以告诉我如何解决我的问题。到目前为止,它打开了相机并且它具有相机的图像视图顶部。但是 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();
    }
}

谢谢

【问题讨论】:

    标签: android camera surfaceview surfaceholder


    【解决方案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();
        }
    };
    

    【讨论】:

    • 这是个老问题,无论如何我都会接受这个答案。
    猜你喜欢
    • 2016-03-30
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多