我在android上开发了一个非常简单的应用程序来获取surfaceView上的网络摄像头屏幕,现在我想将surfaceView作为图像捕获到SDCard上,但在保存下面的图片后我只得到一个黑屏是我的完整应用程序在表面上获取网络摄像头屏幕并从表面视图中捕获图像:
imported packages
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.*;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
在surfaceview上获取网络摄像头屏幕的方法
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
private String mScreenshotPath = Environment.getExternalStorageDirectory() + "/myimages";
//private ArrayList<Element> mElements = new ArrayList<Element>();
private Button takePicture;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.setDrawingCacheEnabled(true);
LayoutInflater controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.control, null);
ViewGroup.LayoutParams layoutParamsControl = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT
,ViewGroup.LayoutParams.FILL_PARENT);
this.addContentView(viewControl,layoutParamsControl);
takePicture = (Button) findViewById(R.id.takepicture);
takePicture.setOnClickListener(this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
camera = Camera.open();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
在同一堂课上,我将surfaceView捕获为图像的方法
@Override
public void onClick(View view) {
//To change body of implemented methods use File | Settings | File Templates.
Toast.makeText(getApplicationContext() , "pucture saved" , Toast.LENGTH_SHORT).show();
saveScreenshot();
//takePicture.setVisibility(View.GONE);
}
private boolean ensureSDCardAccess() {
File file = new File(mScreenshotPath);
if (file.exists()) {
return true;
} else if (file.mkdirs()) {
return true;
}
return false;
}
public void saveScreenshot() {
if (ensureSDCardAccess()) {
//Bitmap bitmap = Bitmap.createBitmap(surfaceView.getWidth(), surfaceView.getHeight(), Bitmap.Config.ARGB_8888);
surfaceHolder.getSurface().freeze();
Bitmap bitmap = surfaceView.getDrawingCache();
Canvas canvas = new Canvas(bitmap);
//surfaceView.draw(canvas);
File file = new File(mScreenshotPath + "/" + System.currentTimeMillis() + ".jpg");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
} catch (FileNotFoundException e) {
Log.e("Panel", "FileNotFoundException", e);
} catch (IOException e) {
Log.e("Panel", "IOEception", e);
}
}
}
布局:
//main.xml
<?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"
>
<SurfaceView
android:id="@+id/camerapreview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>
//control.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="bottom"
>
<Button android:id="@+id/takepicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take picture"
android:layout_gravity="right"
android:layout_margin="10dp"
/>
</LinearLayout>
如您所见,我能够在位图对象中捕获surfaceView,但我的问题是,在将surfaceView 保存在位图对象中期间,在surfaceView 上没有任何网络摄像头屏幕的线索。
我怎样才能有一个持久的surfaceView,这样我就不会错过网络摄像头屏幕?