本文介绍了相机教程安卓(使用surfaceview)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的例子code:
Here is my example code:
package newslab.video.server;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class VideoServer extends Activity implements SurfaceHolder.Callback {
TextView testView;
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
private final String tag = "VideoServer";
Button start, stop;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start = (Button)findViewById(R.id.btn_start);
start.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
start_camera();
}
});
stop = (Button)findViewById(R.id.btn_stop);
stop.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
stop_camera();
}
});
surfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
private void start_camera() {
try{
camera = Camera.open();
}catch(RuntimeException e){
Log.e(tag, "init_camera: " + e);
return;
}
Camera.Parameters param;
param = camera.getParameters();
//modify parameter
param.setPreviewFrameRate(20);
param.setPreviewSize(176, 144);
camera.setParameters(param);
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
Log.e(tag, "init_camera: " + e);
return;
}
}
private void stop_camera() {
camera.stopPreview();
camera.release();
}
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
然而,它只是显示了preVIEW和停止相机。照片是否有任何其他的教程,教进一步处理,比如如何保存图像或位图处理图像?
However, it just shows the preview and stop the camera.
Is there any other tutorial to teach further processing such as how to save the image or process the image with bitmap?
推荐答案
确定。因此,这里的解决方案:
Ok. So here is the solution:
我不加入任何尝试捕捉。这简直是对拍摄图像并将其存储到SD卡。
I am not adding any try catch.This is simply for the capturing the image and storing it into the sdcard.
public class VideoServer extends Activity implements SurfaceHolder.Callback {
TextView testView;
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
PictureCallback rawCallback;
ShutterCallback shutterCallback;
PictureCallback jpegCallback;
private final String tag = "VideoServer";
Button start, stop, capture;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start = (Button)findViewById(R.id.btn_start);
start.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View arg0) {
start_camera();
}
});
stop = (Button)findViewById(R.id.btn_stop);
capture = (Button) findViewById(R.id.capture);
stop.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View arg0) {
stop_camera();
}
});
capture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
captureImage();
}
});
surfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.d("Log", "onPictureTaken - raw");
}
};
/** Handles data for jpeg picture */
shutterCallback = new ShutterCallback() {
public void onShutter() {
Log.i("Log", "onShutter'd");
}
};
jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(String.format(
"/sdcard/%d.jpg", System.currentTimeMillis()));
outStream.write(data);
outStream.close();
Log.d("Log", "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d("Log", "onPictureTaken - jpeg");
}
};
}
private void captureImage() {
// TODO Auto-generated method stub
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
private void start_camera()
{
try{
camera = Camera.open();
}catch(RuntimeException e){
Log.e(tag, "init_camera: " + e);
return;
}
Camera.Parameters param;
param = camera.getParameters();
//modify parameter
param.setPreviewFrameRate(20);
param.setPreviewSize(176, 144);
camera.setParameters(param);
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
//camera.takePicture(shutter, raw, jpeg)
} catch (Exception e) {
Log.e(tag, "init_camera: " + e);
return;
}
}
private void stop_camera()
{
camera.stopPreview();
camera.release();
}
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
不要preSS caputure 按钮之前在开始,因为我没有签。我只是想告诉你,如何捕捉和存储图像到 SD卡
Don't press caputure button before the start,because I didn't checked it. I just wanted to show you that how to capture and store image into the sdcard.
这篇关于相机教程安卓(使用surfaceview)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!