Android拍照片

时间:2016-02-27 22:18:14

标签: java android

所以我正在编写一个用于立即捕获图像的类,并返回图像的字节数组:
    import android.content.Context;     import android.content.pm.PackageManager;     import android.hardware.Camera;                 byteData = data;                 byteDataFilled = true;                 camera.release();             }         };

    public oldCameraAPI(Context appContext)
    {
        mainContext = appContext;
    }

    public static Camera getCameraInstance()
    {
        Camera c = null;
        try
        {
            c = Camera.open(); // attempt to get a Camera instance
        } catch (Exception e)
        {
            // Camera is not available (in use or does not exist)
            Log.e("oldCameraAPI:gCI", "Camera is in use or doesn't exist");
            return null;
        }
        return c; // returns null if camera is unavailable
    }

    private static File getOutputMediaFile(int type)
    {
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.

        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "MyCameraApp");
        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists())
        {
            if (!mediaStorageDir.mkdirs())
            {
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE)
        {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_" + timeStamp + ".jpg");
        }
        else if (type == MEDIA_TYPE_VIDEO)
        {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "VID_" + timeStamp + ".mp4");
        }
        else
        {
            return null;
        }

        return mediaFile;
    }

    public byte[] captureImage(Context context)
    {
        String TAG = "oldCameraAPI:captureImage";
        if (!checkCameraHardware(context))
        {
            Log.e(TAG, "No camera on this device!");
        }

        Camera theCamera = getCameraInstance();
        if (theCamera != null)
        {
            try
            {
                SurfaceView view = new SurfaceView(mainContext);
                theCamera.setPreviewDisplay(view.getHolder());
            } catch (IOException e)
            {
                e.printStackTrace();
            }
            theCamera.startPreview();
            Log.w(TAG, "Preview started");
            theCamera.takePicture(null, mPicture, null, null);
            Log.w(TAG, "Picture taken");
            theCamera.release();
            Log.w(TAG, "Camera released");
        }
        while (!byteDataFilled)
        {
            ;
        }
        byteDataFilled = false;
        return byteData;
    }

    private boolean checkCameraHardware(Context context)
    {

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceView;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class oldCameraAPI extends AppCompatActivity
{
    public static final int MEDIA_TYPE_IMAGE = 1;
    public static final int MEDIA_TYPE_VIDEO = 2;
    protected Context mainContext;
    byte[] byteData = null;
    boolean byteDataFilled = false;

    private Camera.PictureCallback jpegCallback = new Camera.PictureCallback()
    {
        @Override
        public void onPictureTaken(byte[] data, Camera camera)
        {
            //byteDataFilled = true;
        }
    };
    private Camera.PictureCallback mPicture = new Camera.PictureCallback()
    {

        @Override
        public void onPictureTaken(byte[] data, Camera camera)
        {
            /*String TAG = "PictureCallback:onPictureTaken";
            File pictureFile = getOutputMediaFile(1);
            if (pictureFile == null){
                Log.d(TAG, "Error creating media file, check storage permissions: ");
                return;
            }

            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {
                Log.d(TAG, "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d(TAG, "Error accessing file: " + e.getMessage());
            }*/         // this device has a camera
        // no camera on this device
        return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
    }
}

所以在我的主要活动中,我基本上是oldCameraAPI oldCam = new oldCameraAPI(this.getApplicationContext())然后是bArray = oldCam.takePicture。但每当我尝试捕获图像时,应用程序始终会抛出错误。我错过了什么?

'复制'的解决方案是在takePicture之前调用startPreview,我已经这样做了 编辑:错误:

3236-3236/com.example.jdoe.pic E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jdoe.pic, PID:3236                                                                           java.lang.RuntimeException: takePicture failed
     at android.hardware.Camera.native_takePicture(Native Method)
     at android.hardware.Camera.takePicture(Camera.java:1434)
     at com.example.jdoe.pic.oldCameraAPI.captureImage(oldCameraAPI.java:55)
     at com.example.jdoe.pic.MainActivity.takeCameraImage(MainActivity.java:132)
     at com.example.jdoe.pic.MainActivity$1.onClick(MainActivity.java:45)
    at android.view.View.performClick(View.java:5204)
     at android.view.View$PerformClick.run(View.java:21154)
     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:5417)
     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 个答案:

没有答案