我有这个活动(MakePhotoActivity)类,当您打开应用程序时需要拍照。我已经设置它使用前置摄像头。在短信上用相机拍摄照片
然后我有另一个班,这是一个SMS广播接收器已经工作。
现在,我想使它在接收短信时,我希望它能够使用当前的课程拍摄照片。但我怎样才能将它们整合在一起?
正如我已经尝试复制方法(surfaceChanged等)到我的广播接收机类,并在短信接收,我放置在onCreate(在MakePhotoActivity)内的代码。它不工作。
public class MakePhotoActivity extends Activity implements SurfaceHolder.Callback
{
//a variable to store a reference to the Image View at the main.xml file
private ImageView iv_image;
//a variable to store a reference to the Surface View at the main.xml file
private SurfaceView sv;
//a bitmap to display the captured image
private Bitmap bmp;
private int cameraId = 0;
//Camera variables
//a surface holder
private SurfaceHolder sHolder;
//a variable to control the camera
private Camera mCamera;
//the camera parameters
private Parameters parameters;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get the Image View at the main.xml file
iv_image = (ImageView) findViewById(R.id.imageView);
//get the Surface View at the main.xml file
sv = (SurfaceView) findViewById(R.id.surfaceView);
//Get a surface
sHolder = sv.getHolder();
//add the callback interface methods defined below as the Surface View callbacks
sHolder.addCallback(this);
//tells Android that this surface will have its data constantly replaced
sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
private int findFrontFacingCamera() {
int cameraId = -1;
// Search for the front facing camera
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
Log.d("A", "Camera found");
cameraId = i;
break;
}
}
return cameraId;
}
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3)
{
//get camera parameters
parameters = mCamera.getParameters();
//set camera parameters
mCamera.setParameters(parameters);
mCamera.startPreview();
//sets what code should be executed after the picture is taken
Camera.PictureCallback mCall = new Camera.PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera)
{
//decode the data obtained by the camera into a Bitmap
bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
//set the iv_image
iv_image.setImageBitmap(bmp);
FileOutputStream outStream = null;
try{
outStream = new FileOutputStream("/sdcard/Image"+System.currentTimeMillis()+".jpg");
outStream.write(data);
outStream.close();
} catch (FileNotFoundException e){
Log.d("CAMERA", e.getMessage());
} catch (IOException e){
Log.d("CAMERA", e.getMessage());
}
}
};
mCamera.takePicture(null, null, mCall);
}
public void surfaceCreated(SurfaceHolder holder)
{
// The Surface has been created, acquire the camera and tell it where
// to draw the preview.
if (mCamera == null) {
cameraId = findFrontFacingCamera();
mCamera = Camera.open(cameraId);
try {
mCamera.setPreviewDisplay(holder);
// TODO test how much setPreviewCallbackWithBuffer is faster
// mCamera.setPreviewCallback((PreviewCallback) this);
} catch (IOException e) {
mCamera.release();
mCamera = null;
}
}
}
public void surfaceDestroyed(SurfaceHolder holder)
{
if (mCamera != null) {
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (mCamera!=null)
{
mCamera.stopPreview();
mCamera.release();
mCamera=null;
}
}
}
更新:这是我所做的。投入服务,但我得到的是说应用程序通过零位面的误差,摄像机服务器死了!死ICamera,错误100
我引用来自http://easyandroidtutorials.blogspot.in/2012/09/capture-image-without-preview-as.html的代码,并做了一些细微的变化,仍然无法正常工作。
public class CameraService extends Service
{
//Camera variables
//a surface holder
private SurfaceHolder sHolder;
//a variable to control the camera
private Camera mCamera;
//the camera parameters
private Parameters parameters;
SurfaceView sv;
private int cameraId = 0;
/** Called when the activity is first created. */
@Override
public void onCreate()
{
super.onCreate();
Log.i("Service", "Service started");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
if (mCamera == null) {
cameraId = findFrontFacingCamera();
mCamera = Camera.open(cameraId);
try {
// Thread.sleep(3000);
sv = new SurfaceView(getApplicationContext());
mCamera.setPreviewDisplay(sv.getHolder());
parameters = mCamera.getParameters();
//set camera parameters
mCamera.setParameters(parameters);
mCamera.startPreview();
mCamera.takePicture(null, null, mCall);
} catch (IOException e) {
// TODO Auto-generated catch block
mCamera.release();
mCamera = null;
e.printStackTrace();
}
//Get a surface
//sHolder = sv.getHolder();
//tells Android that this surface will have its data constantly replaced
// sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
} catch (Exception e) {
e.printStackTrace();
}
return Service.START_STICKY;
}
private int findFrontFacingCamera() {
int cameraId = -1;
// Search for the front facing camera
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
Log.d("A", "Camera found");
cameraId = i;
break;
}
}
return cameraId;
}
Camera.PictureCallback mCall = new Camera.PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera)
{
//decode the data obtained by the camera into a Bitmap
FileOutputStream outStream = null;
try{
outStream = new FileOutputStream("/sdcard/Image.jpg");
outStream.write(data);
outStream.close();
} catch (FileNotFoundException e){
Log.d("CAMERA", e.getMessage());
} catch (IOException e){
Log.d("CAMERA", e.getMessage());
}
}
};
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
任何人有任何帮助吗?谢谢。在开始的活动
采取在服务画面将要求您创建一个虚拟的更多信息
Intent intent = new Intent(this, MakePhotoActivity.class);
startActivity(intent);
检查http://developer.android.com/training/basics/firstapp/starting-activity.html:
你读过[BroadcastReceiver文档](http://developer.android.com/reference/android/content/BroadcastReceiver.html)和[Intent and intent filters](http://developer.android。 com/guide/components/intents-filters.html)都来自Android官方文档? – 2013-04-24 18:38:25
嗨,我不是试图开始短信接收的活动。相反,我试图让它在没有活动类的情况下自动接收短信。 – user1778855 2013-04-24 18:40:31
对不起,现在我明白了。 现在,如果我没有错,理论上说你应该运行[service](http://developer.android.com/guide/components/services.html)来做到这一点,但我不知道为什么你的代码失败了。 正在抛出哪个错误? – 2013-04-24 18:53:02