拍照失败的android

时间:2016-10-27 18:56:03

标签: java android

我有这个代码,当我点击snapit按钮来修正照片时,他们向我显示错误“java.lang.RuntimeException:takePicture failed”。 我尝试在有线电视月之前使用的代码

vpublic class TackPhoto extends AppCompatActivity {
//    SurfaceView cameraView;
    customSurfaceView surfaceView;
//    SurfaceHolder surfaceHolder;
    Camera camera;
    Button flipCamera;
    Button snapPhoto ;
    int cameraID = 0;

    private Camera.PictureCallback capturedIt = new Camera.PictureCallback(){

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            if (bitmap == null){
                Toast.makeText(getApplicationContext(), "not taken", Toast.LENGTH_SHORT).show();
            }else {
                .
                .
                .
                .

                Toast.makeText(getApplicationContext(), "taken",Toast.LENGTH_SHORT).show();

            }
            camera.release();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tack_photo);

        try {
            camera = Camera.open(cameraID);
        }
        catch (Exception e){
        }
        surfaceView = new customSurfaceView(this,camera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);

        preview.addView(surfaceView);


        snapPhoto = (Button)findViewById(R.id.snapPhoto);
        snapPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                camera.takePicture(null,null,capturedIt);
            }
        });
    }

}

这是前面的课程

 public class customSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback {

            private Camera camera;
            //private SurfaceHolder surfaceHolder;
            private SurfaceHolder surfaceHolder;
            private Context context;
            boolean previewing = false;


   
            @Override
            public void surfaceCreated(SurfaceHolder holder) {


                try
                {
                    camera = Camera.open(cameraID);
                    camera.setPreviewDisplay(holder);
                }
                catch (IOException exception)
                {
                    camera.release();
                }

                camera.startPreview();
                previewing = true;

            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                if(previewing){
                    camera.stopPreview();
                    previewing = false;
                }

                if (camera != null){
                    try {
                        camera.setPreviewDisplay(holder);
                        Display display = ((WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

                        if(display.getRotation() == Surface.ROTATION_0)
                        {
                            camera.setDisplayOrientation(90);
                        }
                        if(display.getRotation() == Surface.ROTATION_270)
                        {
                            camera.setDisplayOrientation(180);
                        }

                        camera.startPreview();
                        previewing = true;

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

                camera.stopPreview();
                if (camera != null)
                    camera.release();
                camera=null;

            }

            public void flipit(){
                camera.stopPreview();
                camera.release();

                if (cameraID == 0){
                    camera = Camera.open(1);
                    cameraID = 1;
                }else {
                    camera = Camera.open(0);
                    cameraID = 0;
                }
                try
                {

                    camera.setPreviewDisplay(surfaceHolder);
                    camera.setPreviewCallback(this);
                    if (camera != null){
                        try {
                            camera.setPreviewDisplay(surfaceHolder);
                            Display display = ((WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

                            if(display.getRotation() == Surface.ROTATION_0)
                            {
                                camera.setDisplayOrientation(90);
                            }
                            if(display.getRotation() == Surface.ROTATION_270)
                            {
                                camera.setDisplayOrientation(180);
                            }

                            //camera.startPreview();
                            camera.startPreview();
                            previewing = true;

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                catch (IOException exception)
                {
                    camera.release();
                }



            }

            @Override
            public void onPreviewFrame(byte[] data, Camera camera) {

            }
        }

logcat输出

    E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                                 Process: com.example.abcd.abcd.abcd.abcd, PID: 12272
                                                                                                 Theme: themes:{}
                                                                                                 java.lang.RuntimeException: takePicture failed
                                                                                                     at android.hardware.Camera.native_takePicture(Native Method)
                                                                                                     at android.hardware.Camera.takePicture(Camera.java:1492)
                                                                                                     at android.hardware.Camera.takePicture(Camera.java:1436)
                                                                                                     at com.example.abcd.abcd.abcd.abcd.TackPhoto$3.onClick(TackPhoto.java:170)
                                                                                                     at android.view.View.performClick(View.java:5204)
                                                                                                     at android.view.View$PerformClick.run(View.java:21158)
                                                                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5461)
                                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

0 个答案:

没有答案