나는이 액티비티 (MakePhotoActivity) 클래스를 사용하여 애플리케이션을 열 때 사진을 찍습니다. 전방 향 카메라를 사용하도록 설정했습니다.SMS로 사진 찍기
그렇다면 이미 다른 클래스가 있습니다.이 클래스는 이미 작동중인 SMS 브로드 캐스트 수신기입니다.
이제 SMS를 받으면 현재 수업을 사용하여 사진을 찍고 싶습니다. 그러나 어떻게 그들을 함께 통합 할 수 있습니까?
내 방송 수신기 클래스에 메서드 (surfaceChanged 등)를 복사 해보고 sms가 수신함에 따라 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;
}
}
}
업데이트 : 이것은 내가 한 것입니다. 서비스에 넣어,하지만 응용 프로그램이 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;
}
누구에게 도움이 되셨습니까? 감사. 당신의 SMS 방송 리시버의 onReceive 방법
[BroadcastReceiver 설명서] (http://developer.android.com/reference/android/content/BroadcastReceiver.html) 및 [의도 및 의도 필터] (http://developer.android. co.kr/guide/components/intents-filters.html)을 참조하십시오. –
안녕하세요, 저는 SMS 수신시 활동을 시작하려고하지 않습니다. 오히려, 나는 그것이 활동 클래스없이 SMS를받을 때 자동으로 사진을 찍도록 노력하고있다. – user1778855
죄송합니다, 이제 알겠습니다. 내가 틀렸어도 이론에 따르면 [service] (http://developer.android.com/guide/components/services.html)를 실행해야한다고 나와 있지만 왜 그런지는 모르겠다. 코드가 실패했습니다. 어떤 오류가 발생하고 있습니까? –