2014-11-24 41 views
1

嗨,我想实现自定义安卓相机应用程序,其中包括imageview在相机preview.After点击屏幕后,我想存储在SD卡中的图像。到目前为止我已经完成了这部分。但我不能点击SurfaceView和保存image.Can任何人都可以告诉我如何解决我的问题。到目前为止,它打开相机,它具有imageview顶部的相机。但onclick方法不工作。并且任何人都可以告诉我如何保存图像时调用onclick函数。这是mycode。Android自定义相机SurfaceView OnClick将图像保存在SD卡上?

这是mainlayout

<?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
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(); 
    } 
}; 
+0

这是老问题了,无论如何,我会接受这个答案。 – Sajithv 2017-05-05 05:11:07