如何解决Android相机setParameters失败

时间:2016-07-09 06:46:04

标签: android camera android-camera surfaceview surfaceholder

我们在应用程序中使用了相机功能。应用程序也将在Google App Store中发布。但我们面临的新问题是setParameters在我们的应用程序中失败了一些设备只有moto g第二代。请在下面找到错误控制台和SurfaceView编码。如何解决这个问题请指导我。感谢

07-09 11:54:08.460: E/AndroidRuntime(13821): FATAL EXCEPTION: main
07-09 11:54:08.460: E/AndroidRuntime(13821): Process: com.example.surfaceviewtest, PID: 13821
07-09 11:54:08.460: E/AndroidRuntime(13821): java.lang.RuntimeException: setParameters failed
07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.hardware.Camera.native_setParameters(Native Method)
07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.hardware.Camera.setParameters(Camera.java:1962)
07-09 11:54:08.460: E/AndroidRuntime(13821):    at com.example.surfaceviewtest.Test.surfaceChanged(Test.java:58)
07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.view.SurfaceView.updateWindow(SurfaceView.java:594)
07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:177)
07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2067)
07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119)
07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6060)
07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.view.Choreographer.doCallbacks(Choreographer.java:670)
07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.view.Choreographer.doFrame(Choreographer.java:606)
07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.os.Handler.handleCallback(Handler.java:746)
07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.os.Handler.dispatchMessage(Handler.java:95)
07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.os.Looper.loop(Looper.java:148)
07-09 11:54:08.460: E/AndroidRuntime(13821):    at android.app.ActivityThread.main(ActivityThread.java:5443)
07-09 11:54:08.460: E/AndroidRuntime(13821):    at java.lang.reflect.Method.invoke(Native Method)
07-09 11:54:08.460: E/AndroidRuntime(13821):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
07-09 11:54:08.460: E/AndroidRuntime(13821):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#ffffffff"
    >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        android:orientation="vertical"
        android:id="@+id/shareLayout"


        >

        <FrameLayout
            android:id="@+id/FrameLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >

            <com.example.surfaceviewtest.Test
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"

                />


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/weather"
                android:orientation="vertical"
                  android:background="@android:color/transparent"

                android:visibility="visible"
                >

               <!--   android:background="#4FFFFFFF" -->

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="6"

                    />

                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="fill_parent"
                    android:layout_height="453dp"
                    android:adjustViewBounds="true"
                    android:background="@android:color/transparent"
                    android:scaleType="matrix"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="This is SurfaceView Test"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="Answerofgod.blogspot.kr"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="Answerofgod.tistory.com"
                    />
            </LinearLayout>

        </FrameLayout>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"

        android:gravity="center_horizontal">

        <Button
            android:id="@+id/share"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="share"
            android:gravity="center"
            />

        <Button
            android:id="@+id/exit"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="explore"
            android:gravity="center"
            />
        <ToggleButton
        android:id="@+id/onOffFlashlight"
        android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
        android:textOn="Turn Off"
        android:textOff="Turn On"
        android:checked="false"
        android:text="Turn On/Off Camera LED/ Flashlight Android" />
    </LinearLayout> 
</LinearLayout>

Test.java

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

    SurfaceHolder holder;   
    Camera cam=null;        
int width;
int height;
private Camera.Size mPreviewSize;

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

        holder=getHolder();
        holder.addCallback(this);

    }



     @Override
      public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
       Camera.Parameters myParameters = cam.getParameters();

       int   wid = ((Activity) getContext()).getWindowManager().getDefaultDisplay().getWidth(); 
       int hei = ((Activity) getContext()).getWindowManager().getDefaultDisplay().getHeight();

       System.out.println("wid"+wid);
       System.out.println("hei"+hei);
       Camera.Size myBestSize = getBestPreviewSize(hei, wid, myParameters);

       if(myBestSize != null){
        myParameters.setPreviewSize(hei, wid);
        cam.setParameters(myParameters);
        cam.setDisplayOrientation(90);
        cam.startPreview();
       // isPreview = true;


       }
      }

     private Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters){
         Camera.Size bestSize = null;
         List<Camera.Size> sizeList = parameters.getSupportedPreviewSizes();

         bestSize = sizeList.get(0);

         for(int i = 1; i < sizeList.size(); i++){
          if((sizeList.get(i).width * sizeList.get(i).height) >
            (bestSize.width * bestSize.height)){
           bestSize = sizeList.get(i);
          }
         }

         return bestSize;
        }





    /*@Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {

        cam.setDisplayOrientation(90);      
        cam.startPreview();                 
    }*/
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        cam=Camera.open();              

        try{
            cam.setPreviewDisplay(holder);  
            cam.setPreviewCallback(this);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        cam.setPreviewCallback(null);
        cam.stopPreview();
        cam.release();                  
        cam=null;

    }

    /*@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
          width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
    }*/


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

        Camera.Parameters params = camera.getParameters();
         Camera.Size previewSize = params.getSupportedPreviewSizes().get(0);
        int wi = previewSize.width;
        //System.out.println("wi======"+wi);
        int he = previewSize.height;
        //params.setPreviewSize(wi, he);
        System.out.println("wi==========="+wi);
        System.out.println("he======"+he);
        int w = params.getPreviewSize().width;
    System.out.println("w=============="+ w);
        int h = params.getPreviewSize().height;
        System.out.println(" h =============="+ h );
        int format = params.getPreviewFormat();


        int  wid = ((Activity) getContext()).getWindowManager().getDefaultDisplay().getWidth(); 
        int hei = ((Activity) getContext()).getWindowManager().getDefaultDisplay().getHeight();




        YuvImage image = new YuvImage(data, format,w, h, null);





        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Rect area = new Rect(0, 0,w, h);
        image.compressToJpeg(area, 100, out);
        Bitmap bm = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());






        Matrix matrix = new Matrix();
        matrix.postRotate(90);
         //mBitmap = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight()gr
            //     mtrxFreeze, true);

        Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
        //Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0,w, he, matrix, true);

        //System.out.println("shareBitmap   getHeight"+rotatedBitmap.getHeight());
        //System.out.println("shareBitmap   getWidth"+rotatedBitmap.getWidth());


        MainActivity.shareBitmap=rotatedBitmap;

    }
}

2 个答案:

答案 0 :(得分:0)

不幸的是,setParameters方法的RuntimeException非常通用,并没有提供足够的失败原因信息。使用以下命令逐个检查所有参数:

parameters.flatten();

答案 1 :(得分:0)

尝试更改代码:

myParameters.setPreviewSize(hei, wid);

到:

myParameters.setPreviewSize(myBestSize.width, myBestSize.height);
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)方法

中的