Android setParameters失败

时间:2014-11-25 01:12:50

标签: android camera crash

我正在尝试通过我的APP拍照,但在某些设备上拍照却失败了一些设备。 我很确定我做得对,但android不接受setParameters。这真的很烦人,不确定为什么在Android平台上开发一些东西太难了。在公司我们正在构建每个应用程序的Android和iPhone版本,但iPhone团队总是在Android之前最多3周完成项目。 我尝试了很多代码,我找到了SOF或其他来源但没有正常工作。

无论如何,这是我拍照的代码

public class TakePicturePortrait extends Activity
{
    private int                 screenW;
    private int                 screenH;

    @Override
    public void onCreate(Bundle b)
    {
        super.onCreate(b);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_fullscreen);

        // Get Screen dimention first.
        getScreenSize();

        picture_guide_layout = (RelativeLayout) findViewById(R.id.picture_guide_layout);
        mySurfaceView = (SurfaceView) findViewById(R.id.surface_view);

        holder = mySurfaceView.getHolder();
        holder.addCallback(mSurfaceListener);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    protected void onResume()
    {
        if (myCamera == null)
            myCamera = getCameraInstance();

        super.onResume();
    }

    @Override
    public void onPause()
    {
        super.onPause();
        releaseCamera();
    }

    private void releaseCamera()
    {
        if (myCamera != null)
        {
            myCamera.setPreviewCallback(null);
            myCamera.release();
            myCamera = null;
        }
    }

    private static Camera getCameraInstance()
    {
        Camera c = null;

        try
        {
            c = Camera.open();
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }

        return c;
    }

    private final ShutterCallback   shutterCallback     = new ShutterCallback()
    {
        public void onShutter()
        {
            AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            mgr.playSoundEffect(AudioManager.FLAG_PLAY_SOUND);
        }
    };

    private SurfaceHolder.Callback  mSurfaceListener    = new SurfaceHolder.Callback()
    {
        public void surfaceCreated(SurfaceHolder holder)
        {
            try
            {
                myCamera.setPreviewDisplay(holder);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }

        public void surfaceDestroyed(SurfaceHolder holder)
        {
        }

        @SuppressLint("InlinedApi")
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
        {
            Parameters mParam = myCamera.getParameters();

            int w = 0;
            int h = 0;

            List<Size> getPictureSize = mParam.getSupportedPictureSizes();

            for(Size s: getPictureSize)
            {
                if(s.width < 1280)
                    break;

                w = s.width;
                h = s.height;
            }

            mParam.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
            mParam.setPictureFormat(ImageFormat.JPEG);
            mParam.setJpegQuality(90);
            mParam.setPictureSize(w, h);
            mParam.setRotation(fixPictureOrientation());

            myCamera.stopPreview();
            myCamera.setParameters(mParam);

            Display display = getWindowManager().getDefaultDisplay();

            if (display.getRotation() == Surface.ROTATION_0)
                myCamera.setDisplayOrientation(90);
        }
    };

    private int fixPictureOrientation()
    {
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, info);
        int rotation = getWindowManager().getDefaultDisplay().getRotation();
        int degrees = 0;
        switch (rotation)
        {
            case Surface.ROTATION_0 :
                degrees = 0;
                break; // Natural orientation
            case Surface.ROTATION_90 :
                degrees = 90;
                break; // Landscape left
            case Surface.ROTATION_180 :
                degrees = 180;
                break;// Upside down
            case Surface.ROTATION_270 :
                degrees = 270;
                break;// Landscape right
        }

        int rotate = (info.orientation - degrees + 360) % 360;

        return rotate;
    }

    private PictureCallback mPictureListener    = new PictureCallback()
    {
        @Override
        public void onPictureTaken(byte[] data, Camera camera)
        {
            data = getRotatedData(data);

            String fileName = (System.currentTimeMillis() / 1000) + ".jpg";

            try
            {
                File file = new File(PATH + "/" + fileName);

                FileOutputStream out = new FileOutputStream(file);
                out.write(data);

                if (out != null)
                    out.close();
            }
            catch (Exception e)
            {
            }

            myCamera.stopPreview();
        }
    };

    public void takePicture(View v)
    {
        if (myCamera != null)
            myCamera.takePicture(shutterCallback, null, mPictureListener);
    }

    private void getScreenSize()
    {
        screenW = getResources().getDisplayMetrics().widthPixels;
        screenH = getResources().getDisplayMetrics().heightPixels;
    }

    private byte[] getRotatedData(byte[] data)
    {
        byte[] rotatedData = data;

        Bitmap originalBmp = BitmapFactory.decodeByteArray(data, 0, data.length, null);
        int originalW = originalBmp.getWidth();
        int originalH = originalBmp.getHeight();

        Matrix mat = new Matrix();
        mat.postRotate(0);

        Bitmap rotetedBmp = Bitmap.createBitmap(originalBmp, 0, 0, originalW, originalH, mat, true);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        rotetedBmp.compress(CompressFormat.JPEG, 90, bos);

        try
        {
            bos.flush();
        }
        catch (IOException e1)
        {
            e1.printStackTrace();
        }

        rotatedData = bos.toByteArray();
        try
        {
            bos.close();
        }
        catch (IOException e1)
        {
            e1.printStackTrace();
        }

        return rotatedData;
    }
}

在这个代码完美地工作三星S3,但在三星S4失败。 LogCat说问题是myCamera.setParameters(mParam);

这是ErrLog输出

11-25 10:01:40.737: E/AndroidRuntime(4257): FATAL EXCEPTION: main
11-25 10:01:40.737: E/AndroidRuntime(4257): java.lang.RuntimeException: setParameters failed
11-25 10:01:40.737: E/AndroidRuntime(4257): at android.hardware.Camera.native_setParameters(Native Method)
11-25 10:01:40.737: E/AndroidRuntime(4257): at android.hardware.Camera.setParameters(Camera.java:1003)
11-25 10:01:40.737: E/AndroidRuntime(4257): at main_app.TakePictureLandscape$2.surfaceChanged(TakePictureLandscape.java:284)
11-25 10:01:40.737: E/AndroidRuntime(4257): at android.view.SurfaceView.updateWindow(SurfaceView.java:619)
11-25 10:01:40.737: E/AndroidRuntime(4257): at android.view.SurfaceView.dispatchDraw(SurfaceView.java:398)
11-25 10:01:40.737: E/AndroidRuntime(4257): at android.view.ViewGroup.drawChild(ViewGroup.java:1669)
11-25 10:01:40.737: E/AndroidRuntime(4257): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1398)
11-25 10:01:40.737: E/AndroidRuntime(4257): at android.view.ViewGroup.drawChild(ViewGroup.java:1669)
11-25 10:01:40.737: E/AndroidRuntime(4257): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1398)
11-25 10:01:40.737: E/AndroidRuntime(4257): at android.view.View.draw(View.java:6883)
11-25 10:01:40.737: E/AndroidRuntime(4257): at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-25 10:01:40.737: E/AndroidRuntime(4257): at android.view.ViewGroup.drawChild(ViewGroup.java:1671)
11-25 10:01:40.737: E/AndroidRuntime(4257): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1398)
11-25 10:01:40.737: E/AndroidRuntime(4257): at android.view.View.draw(View.java:6883)
11-25 10:01:40.737: E/AndroidRuntime(4257): at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-25 10:01:40.737: E/AndroidRuntime(4257): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2371)
11-25 10:01:40.737: E/AndroidRuntime(4257): at android.view.ViewRoot.draw(ViewRoot.java:1531)
11-25 10:01:40.737: E/AndroidRuntime(4257): at android.view.ViewRoot.performTraversals(ViewRoot.java:1261)
11-25 10:01:40.737: E/AndroidRuntime(4257): at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
11-25 10:01:40.737: E/AndroidRuntime(4257): at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 10:01:40.737: E/AndroidRuntime(4257): at android.os.Looper.loop(Looper.java:130)
11-25 10:01:40.737: E/AndroidRuntime(4257): at android.app.ActivityThread.main(ActivityThread.java:3705)
11-25 10:01:40.737: E/AndroidRuntime(4257): at java.lang.reflect.Method.invokeNative(Native Method)
11-25 10:01:40.737: E/AndroidRuntime(4257): at java.lang.reflect.Method.invoke(Method.java:507)
11-25 10:01:40.737: E/AndroidRuntime(4257): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-25 10:01:40.737: E/AndroidRuntime(4257): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-25 10:01:40.737: E/AndroidRuntime(4257): at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

相机应用程序总是很烦人,因为它真的取决于每个硬件。所以,我认为这篇文章的问题

        mParam.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
        mParam.setPictureFormat(ImageFormat.JPEG);
        mParam.setJpegQuality(90);
        mParam.setPictureSize(w, h);
        mParam.setRotation(fixPictureOrientation());

例如,如果设备不支持对焦模式图片,那么setFocusMode()会使setParameter失败。因此,请确保设备在setParameter之前支持它, 那样:if(focusModes.contains(Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) { //set }

尝试评论并运行上面的每一行,看看哪条线路出问题。