我在Android上开发了一个非常简单的应用程序,以便在surfaceView上获取网络摄像头屏幕,现在我想将surfaceView作为图像捕获到SDCard上但是我在保存下面的图片之后只得到一个黑屏是我的完整的应用程序,用于获取表面上的网络摄像头屏幕,并从surfaceView中捕获图像:
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所以我不会错过网络摄像头屏幕?
答案 0 :(得分:1)
我有一个类似的事情,我需要用叠加来保存我的图像。我通过首先保存捕获的图像,然后将其放回我的视图,然后将其保存为位图来实现此目的。它足以满足我的需要。