Android 2.3.3上的图片挂起

who*_*who 10 camera android

我有一些拍照的代码在Android 2.1和2.2中有效.但这些代码在Android 2.3上爆发.花了一些时间来解决这个徒劳的问题后,我想在这里寻求帮助.

我拍照的代码流是这样的:

创建一个类Camlayer扩展了SurfaceView

public class CamLayer extends SurfaceView implements SurfaceHolder.Callback {
    private void init(Context context){
        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mCamera = Camera.open();
    }

    public CamLayer(Context context) {
        super(context);
        init(context);
    }

    public CamLayer(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        Log.i(TAG+".surfaceChanged", "being called!");
        Log.i(TAG+".surfaceChanged", "w="+w); 
        Log.i(TAG+".surfaceChanged", "h="+h);
        if (isPreviewRunning) {
            mCamera.stopPreview();
        }

        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.setPreviewCallback(mPreviewCallback);
        } catch (IOException e) {
            Log.e(TAG+".surfaceCreated", "mCamera.setPreviewDisplay(holder);");
        }

        Camera.Parameters p = mCamera.getParameters();  
        setOptimalSize(p, w, h, SIZEOFPREVIEW);
        setOptimalSize(p, w, h, SIZEOFPICTURE);
        mCamera.setParameters(p);

        mCamera.startPreview();
        isPreviewRunning = true;
    }

    public void takePicture(){
        Log.i(TAG+".takePicture", "being called!");
        mCamera.takePicture(null, null, mPictureCallback);
        Log.i(TAG+".takePicture", "call ended!");
    }
}
Run Code Online (Sandbox Code Playgroud)

CamLayer.takePicture() 将由外部类调用以启动.

问题是在Android 2.3.3中takePicture会挂起,因此会发现ANR问题.在/data/anr/traces.txt,下面找到.如你所见,native_takePicture永不回归.

达尔维克线:

(mutexes: tll=0 tsl=0 tscl=0 ghl=0 hwl=0 hwll=0)
"main" prio=5 tid=1 NATIVE

  | group="main" sCount=1 dsCount=0 obj=0x40022170 self=0xce68

  | sysTid=2411 nice=0 sched=0/0 cgrp=default handle=-1345006464

  at android.hardware.Camera.native_takePicture(Native Method)

  at android.hardware.Camera.takePicture(Camera.java:746)

  at android.hardware.Camera.takePicture(Camera.java:710)

  at oms.cj.tube.camera.CamLayer.takePicture(CamLayer.java:256)

  at oms.cj.tube.camera.DefineColor.takePicture(DefineColor.java:61)

  at oms.cj.tube.camera.DefineColor.onKeyUp(DefineColor.java:71)

  at android.view.KeyEvent.dispatch(KeyEvent.java:1280)

  at android.app.Activity.dispatchKeyEvent(Activity.java:2078)

  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:16
66)
  at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2571)

  at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2546)

  at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)

  at android.os.Handler.dispatchMessage(Handler.java:99)

  at android.os.Looper.loop(Looper.java:123)

  at android.app.ActivityThread.main(ActivityThread.java:3691)

  at java.lang.reflect.Method.invokeNative(Native Method)

  at java.lang.reflect.Method.invoke(Method.java:507)

  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)

  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)

  at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)

有没有人有同样的问题?并知道如何解决它?

Mar*_*sny 7

我还观察了mCamera.takePicture(null,null,handler)来冻结.我试着在调用takePicture()之前清除预览处理程序:mCamera.setPreviewCallback(null),现在它可以工作了.


Jor*_*cia 5

今天我在使用Android 2.3.3在Samsung Exhibit 4G上测试我们的应用时遇到了完全相同的问题,并使用解决方法解决了它.

我不再调用takepicture了,而是使用最后一个预览回调作为图片.

问题是预览回调使用NV21格式发送数据缓冲区.

所以你必须使用这个过程转换图像:NV21 - > RGB - >加载位图 - >压缩到JPEG

我们的代码现在看起来像这样:

    camera.setPreviewCallback(new PreviewCallback() {

        @Override
        public synchronized void onPreviewFrame(byte[] data, Camera arg1) {
            if (!mTakePicture) {
                CameraPreview.this.invalidate();
            } else {

                if (mTakePictureCallback != null && !mPictureTaken) {
                    int rgb[] = new int[previewSize.width*previewSize.height];
                    decodeYUV420SP(rgb, data, previewSize.width, previewSize.height);
                    Bitmap memoryImage = Bitmap.createBitmap(rgb, previewSize.width, previewSize.height, Bitmap.Config.ARGB_8888);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    memoryImage.compress(CompressFormat.JPEG, 100, baos);
                    shutterSound();
                    setBackgroundDrawable(new BitmapDrawable(getContext().getResources(), memoryImage));
                    mTakePictureCallback.onPictureTaken(baos.toByteArray(), arg1);
                }
                mPictureTaken = true;
                camera.stopPreview();
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

decodeYUV420SP的代码在http://www.41post.com/3470/programming/android-retrieving-the-camera-preview-as-a-pixel-array,他们在Ketai http://code.google上找到了它. COM/p /科泰/

拍摄照片时,只需将mTakePicture变量设置为true即可

我正在研究一个更好的版本,但这应该让你去.


who*_*who 0

问题在于以下代码的编写。

  1. 定义了一个 PreviewCallback,

    PreviewCallback mPreviewCallback = new PreviewCallback() {
    
        @Override
            public void onPreviewFrame(byte[] data, Camera camera) {
                //Log.i(TAG+".mPreviewCallback.onPreviewFrame", "being called!");
            }
    };
    
    Run Code Online (Sandbox Code Playgroud)
  2. mCamera.setPreviewCallback(mPreviewCallback);

  3. mCamera.takePicture()

这在 2.1/2.2 中有效,但在 2.3 中无效。

不确定 Android 团队是否支持这种使用相机的方式。如果上述流程符合预期,那么 Android 团队应该修复此问题。

  • 因此,如果您取出此代码,您的相机活动是否有效并且是否可以拍摄照片? (2认同)