如何让android相机在多个方向上工作

发布于 2025-01-08 02:28:13 字数 19057 浏览 0 评论 0原文

好的,我有这个相机代码 我想让它在所有方向上都能发挥作用。 在 android 2.2/2.3.3/3.2 上,

我在想类似的事情

if(Build.VERSION.SDK_INT == 8)
do v8 work
else if(Build.VERSION.SDK_INT == 10)
do v10 work
else if(Build.VERSION.SDK_INT == 14)
do v14 work

,但我没有任何运气。 我尝试的一切都失败了。 请帮忙。

Activity.java

package com.ice_os.android.kamura;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;

public class Activity extends Activity {
    private static final String TAG = "Kamura";
    Camera camera;
    Preview preview;
    Button scan;
    Button preferences;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        if(camera != null)
        {
            camera.release();
            camera = null;
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        scan = (Button) findViewById(R.id.scan);
        scan.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                preview.camera.takePicture(shutterCallback, rawCallback,
                        jpegCallback);
            }
        });
        preferences = (Button)findViewById(R.id.preferences);
        preferences.setOnClickListener( new OnClickListener(){

            @Override
            public void onClick(View v) {
                Intent PreferencesIntent = new Intent(AllergyDetektive.this,Preferences.class);
                startActivity(PreferencesIntent);

            }

        });
        preview = new Preview(this);
        ((FrameLayout) findViewById(R.id.preview)).addView(preview);



        Log.d(TAG, "onCreate'd");

    }

    ShutterCallback shutterCallback = new ShutterCallback() {
        public void onShutter() {
            Log.d(TAG, "onShutter'd");
        }
    };

    /** Handles data for raw picture */
    PictureCallback rawCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            Log.d(TAG, "onPictureTaken - raw");
        }
    };

    /** Handles data for jpeg picture */
    PictureCallback jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            FileOutputStream outStream = null;
            try {

                outStream = new FileOutputStream(String.format(
                        "/sdcard/%d.jpg", System.currentTimeMillis()));
                outStream.write(data);
                outStream.close();
                Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            }
            Log.d(TAG, "onPictureTaken - jpeg");
        }
    };  

//  @Override
//  public void onPause(){
//      super.onPause();
//      camera.lock();
//      if(camera != null)
//      {
//          camera.release();
//          camera = null;
//      }
//      
//  }

}

Preview.java

package com.ice_os.android.kamura;


import java.io.IOException;
import java.lang.reflect.Method;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

class Preview extends SurfaceView implements SurfaceHolder.Callback {
    private static final String TAG = "Preview";

    SurfaceHolder mHolder;
    public Camera camera;

    Preview(Context context) {
        super(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);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
        camera = Camera.open();
        try {
            camera.setPreviewDisplay(holder);

            camera.setPreviewCallback(new PreviewCallback() {

                public void onPreviewFrame(byte[] data, Camera arg1) {
                    try {
                        Log.d(TAG, "onPreviewFrame - wrote bytes: "
                                + data.length);
                    }
                    finally {
                    }
                    Preview.this.invalidate();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        // Because the CameraDevice object is not a shared resource, it's very
        // important to release it when the activity is paused.
        camera.stopPreview();
        camera.release();
        camera = null;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        Camera.Parameters parameters = camera.getParameters();
//        if (Build.VERSION.SDK_INT >= 8)
//              setDisplayOrientation(camera, 0);
//          else
//          {
//              if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
//              {
//                  parameters.set("orientation", "portrait");
//                  parameters.set("rotation", 90);
//              }
//              if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
//              {
//                  parameters.set("orientation", "landscape");
//                  parameters.set("rotation", 90);
//              }
//          }  

        parameters.setPreviewSize(w, h);
        parameters.set("orientation","landscape");
        parameters.set("rotation", 0);
        camera.setParameters(parameters);
        camera.startPreview();
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        Paint p = new Paint(Color.RED);
        Log.d(TAG, "draw");
        canvas.drawText("PREVIEW", canvas.getWidth() / 2,
                canvas.getHeight() / 2, p);
    }

    protected void setDisplayOrientation(Camera camera, int angle){
        Method downPolymorphic;
        try
        {
            downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class });
            if (downPolymorphic != null)
                downPolymorphic.invoke(camera, new Object[] { angle });
        }
        catch (Exception e1)
        {
        }
    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ice_os.android.allergy_detektive"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature android:name="android.hardware.camera"/>
    <uses-feature android:name="android.hardware.camera.autofocus"/>
    <uses-permission android:name="android.permission.FLASHLIGHT" android:required="false"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Activity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Preferences" >
        </activity>
    </application>

</manifest>

main.xml

<Button
    android:id="@+id/scan"
    style="@style/ButtonText"
    android:background="@drawable/black_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/preferences"
    android:text="klick" />

<Button
    android:id="@+id/preferences"
    style="@style/ButtonText"
    android:background="@drawable/black_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="@string/preferences" />

<FrameLayout
    android:id="@+id/preview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/scan" >

</FrameLayout>

log.txt

02-25 08:33:30.623: D/Kamura(355): onCreate'd
02-25 08:33:32.012: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:32.362: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:32.593: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:32.952: D/dalvikvm(355): GC_FOR_MALLOC freed 930K, 55% free 3027K/6727K, external 1625K/2137K, paused 117ms
02-25 08:33:33.062: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:33.283: D/dalvikvm(355): GC_FOR_MALLOC freed 438K, 56% free 3026K/6727K, external 1625K/2137K, paused 142ms
02-25 08:33:33.413: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:33.693: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 57ms
02-25 08:33:33.703: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:33.922: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 49ms
02-25 08:33:33.932: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:34.152: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 48ms
02-25 08:33:34.162: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:34.393: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 58ms
02-25 08:33:34.413: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:34.633: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 45ms
02-25 08:33:34.653: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:34.864: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:34.882: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:35.112: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 46ms
02-25 08:33:35.132: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:35.344: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:35.363: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:35.603: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 55ms
02-25 08:33:35.623: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:35.842: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 50ms
02-25 08:33:35.862: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:36.092: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 57ms
02-25 08:33:36.112: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:36.323: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:36.343: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:36.573: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:36.593: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:36.802: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 48ms
02-25 08:33:36.822: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:37.052: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 48ms
02-25 08:33:37.073: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:37.293: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 46ms
02-25 08:33:37.303: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:37.533: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 57ms
02-25 08:33:37.553: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:37.763: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:37.782: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:37.992: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 48ms
02-25 08:33:38.012: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:38.253: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 50ms
02-25 08:33:38.274: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:38.504: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:38.523: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:38.733: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 46ms
02-25 08:33:38.753: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:38.972: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:38.982: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:39.213: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 45ms
02-25 08:33:39.223: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:39.453: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 59ms
02-25 08:33:39.473: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:39.693: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 56ms
02-25 08:33:39.713: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:39.922: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:39.942: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:40.162: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:40.172: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:40.403: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:40.433: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:40.653: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 48ms
02-25 08:33:40.663: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:40.892: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:40.902: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:41.132: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:41.153: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:41.364: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:41.383: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:41.623: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 55ms
02-25 08:33:41.643: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:41.862: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 49ms
02-25 08:33:41.882: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:42.113: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 54ms
02-25 08:33:42.152: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:42.912: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 563ms
02-25 08:33:43.393: D/Kamura(355): onCreate'd
02-25 08:33:43.465: D/AndroidRuntime(355): Shutting down VM
02-25 08:33:43.465: W/dalvikvm(355): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-25 08:33:43.483: E/AndroidRuntime(355): FATAL EXCEPTION: main
02-25 08:33:43.483: E/AndroidRuntime(355): java.lang.RuntimeException: Method called after release()
02-25 08:33:43.483: E/AndroidRuntime(355):  at android.hardware.Camera.setHasPreviewCallback(Native Method)
02-25 08:33:43.483: E/AndroidRuntime(355):  at android.hardware.Camera.access$600(Camera.java:114)
02-25 08:33:43.483: E/AndroidRuntime(355):  at android.hardware.Camera$EventHandler.handleMessage(Camera.java:545)
02-25 08:33:43.483: E/AndroidRuntime(355):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 08:33:43.483: E/AndroidRuntime(355):  at android.os.Looper.loop(Looper.java:123)
02-25 08:33:43.483: E/AndroidRuntime(355):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-25 08:33:43.483: E/AndroidRuntime(355):  at java.lang.reflect.Method.invokeNative(Native Method)
02-25 08:33:43.483: E/AndroidRuntime(355):  at java.lang.reflect.Method.invoke(Method.java:507)
02-25 08:33:43.483: E/AndroidRuntime(355):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-25 08:33:43.483: E/AndroidRuntime(355):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-25 08:33:43.483: E/AndroidRuntime(355):  at dalvik.system.NativeStart.main(Native Method)
02-25 08:33:46.753: I/Process(355): Sending signal. PID: 355 SIG: 9

OK I have this camera code
I want to make it so that it will work in all orientations.
and on android 2.2/2.3.3/3.2

I was thinking something like

if(Build.VERSION.SDK_INT == 8)
do v8 work
else if(Build.VERSION.SDK_INT == 10)
do v10 work
else if(Build.VERSION.SDK_INT == 14)
do v14 work

but I'm not having any luck.
Everything I try fails.
Please help.

Activity.java

package com.ice_os.android.kamura;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;

public class Activity extends Activity {
    private static final String TAG = "Kamura";
    Camera camera;
    Preview preview;
    Button scan;
    Button preferences;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        if(camera != null)
        {
            camera.release();
            camera = null;
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        scan = (Button) findViewById(R.id.scan);
        scan.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                preview.camera.takePicture(shutterCallback, rawCallback,
                        jpegCallback);
            }
        });
        preferences = (Button)findViewById(R.id.preferences);
        preferences.setOnClickListener( new OnClickListener(){

            @Override
            public void onClick(View v) {
                Intent PreferencesIntent = new Intent(AllergyDetektive.this,Preferences.class);
                startActivity(PreferencesIntent);

            }

        });
        preview = new Preview(this);
        ((FrameLayout) findViewById(R.id.preview)).addView(preview);



        Log.d(TAG, "onCreate'd");

    }

    ShutterCallback shutterCallback = new ShutterCallback() {
        public void onShutter() {
            Log.d(TAG, "onShutter'd");
        }
    };

    /** Handles data for raw picture */
    PictureCallback rawCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            Log.d(TAG, "onPictureTaken - raw");
        }
    };

    /** Handles data for jpeg picture */
    PictureCallback jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            FileOutputStream outStream = null;
            try {

                outStream = new FileOutputStream(String.format(
                        "/sdcard/%d.jpg", System.currentTimeMillis()));
                outStream.write(data);
                outStream.close();
                Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            }
            Log.d(TAG, "onPictureTaken - jpeg");
        }
    };  

//  @Override
//  public void onPause(){
//      super.onPause();
//      camera.lock();
//      if(camera != null)
//      {
//          camera.release();
//          camera = null;
//      }
//      
//  }

}

Preview.java

package com.ice_os.android.kamura;


import java.io.IOException;
import java.lang.reflect.Method;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

class Preview extends SurfaceView implements SurfaceHolder.Callback {
    private static final String TAG = "Preview";

    SurfaceHolder mHolder;
    public Camera camera;

    Preview(Context context) {
        super(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);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
        camera = Camera.open();
        try {
            camera.setPreviewDisplay(holder);

            camera.setPreviewCallback(new PreviewCallback() {

                public void onPreviewFrame(byte[] data, Camera arg1) {
                    try {
                        Log.d(TAG, "onPreviewFrame - wrote bytes: "
                                + data.length);
                    }
                    finally {
                    }
                    Preview.this.invalidate();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        // Because the CameraDevice object is not a shared resource, it's very
        // important to release it when the activity is paused.
        camera.stopPreview();
        camera.release();
        camera = null;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        Camera.Parameters parameters = camera.getParameters();
//        if (Build.VERSION.SDK_INT >= 8)
//              setDisplayOrientation(camera, 0);
//          else
//          {
//              if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
//              {
//                  parameters.set("orientation", "portrait");
//                  parameters.set("rotation", 90);
//              }
//              if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
//              {
//                  parameters.set("orientation", "landscape");
//                  parameters.set("rotation", 90);
//              }
//          }  

        parameters.setPreviewSize(w, h);
        parameters.set("orientation","landscape");
        parameters.set("rotation", 0);
        camera.setParameters(parameters);
        camera.startPreview();
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        Paint p = new Paint(Color.RED);
        Log.d(TAG, "draw");
        canvas.drawText("PREVIEW", canvas.getWidth() / 2,
                canvas.getHeight() / 2, p);
    }

    protected void setDisplayOrientation(Camera camera, int angle){
        Method downPolymorphic;
        try
        {
            downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class });
            if (downPolymorphic != null)
                downPolymorphic.invoke(camera, new Object[] { angle });
        }
        catch (Exception e1)
        {
        }
    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ice_os.android.allergy_detektive"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature android:name="android.hardware.camera"/>
    <uses-feature android:name="android.hardware.camera.autofocus"/>
    <uses-permission android:name="android.permission.FLASHLIGHT" android:required="false"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Activity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Preferences" >
        </activity>
    </application>

</manifest>

main.xml

<Button
    android:id="@+id/scan"
    style="@style/ButtonText"
    android:background="@drawable/black_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/preferences"
    android:text="klick" />

<Button
    android:id="@+id/preferences"
    style="@style/ButtonText"
    android:background="@drawable/black_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="@string/preferences" />

<FrameLayout
    android:id="@+id/preview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/scan" >

</FrameLayout>

log.txt

02-25 08:33:30.623: D/Kamura(355): onCreate'd
02-25 08:33:32.012: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:32.362: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:32.593: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:32.952: D/dalvikvm(355): GC_FOR_MALLOC freed 930K, 55% free 3027K/6727K, external 1625K/2137K, paused 117ms
02-25 08:33:33.062: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:33.283: D/dalvikvm(355): GC_FOR_MALLOC freed 438K, 56% free 3026K/6727K, external 1625K/2137K, paused 142ms
02-25 08:33:33.413: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:33.693: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 57ms
02-25 08:33:33.703: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:33.922: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 49ms
02-25 08:33:33.932: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:34.152: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 48ms
02-25 08:33:34.162: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:34.393: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 58ms
02-25 08:33:34.413: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:34.633: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 45ms
02-25 08:33:34.653: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:34.864: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:34.882: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:35.112: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 46ms
02-25 08:33:35.132: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:35.344: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:35.363: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:35.603: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 55ms
02-25 08:33:35.623: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:35.842: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 50ms
02-25 08:33:35.862: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:36.092: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 57ms
02-25 08:33:36.112: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:36.323: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:36.343: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:36.573: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:36.593: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:36.802: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 48ms
02-25 08:33:36.822: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:37.052: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 48ms
02-25 08:33:37.073: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:37.293: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 46ms
02-25 08:33:37.303: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:37.533: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 57ms
02-25 08:33:37.553: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:37.763: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:37.782: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:37.992: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 48ms
02-25 08:33:38.012: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:38.253: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 50ms
02-25 08:33:38.274: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:38.504: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:38.523: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:38.733: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 46ms
02-25 08:33:38.753: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:38.972: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:38.982: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:39.213: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 45ms
02-25 08:33:39.223: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:39.453: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 59ms
02-25 08:33:39.473: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:39.693: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 56ms
02-25 08:33:39.713: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:39.922: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:39.942: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:40.162: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:40.172: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:40.403: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:40.433: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:40.653: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 48ms
02-25 08:33:40.663: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:40.892: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:40.902: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:41.132: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:41.153: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:41.364: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 47ms
02-25 08:33:41.383: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:41.623: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 55ms
02-25 08:33:41.643: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:41.862: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 49ms
02-25 08:33:41.882: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:42.113: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 54ms
02-25 08:33:42.152: D/Preview(355): onPreviewFrame - wrote bytes: 447120
02-25 08:33:42.912: D/dalvikvm(355): GC_FOR_MALLOC freed 436K, 56% free 3026K/6727K, external 1625K/2137K, paused 563ms
02-25 08:33:43.393: D/Kamura(355): onCreate'd
02-25 08:33:43.465: D/AndroidRuntime(355): Shutting down VM
02-25 08:33:43.465: W/dalvikvm(355): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-25 08:33:43.483: E/AndroidRuntime(355): FATAL EXCEPTION: main
02-25 08:33:43.483: E/AndroidRuntime(355): java.lang.RuntimeException: Method called after release()
02-25 08:33:43.483: E/AndroidRuntime(355):  at android.hardware.Camera.setHasPreviewCallback(Native Method)
02-25 08:33:43.483: E/AndroidRuntime(355):  at android.hardware.Camera.access$600(Camera.java:114)
02-25 08:33:43.483: E/AndroidRuntime(355):  at android.hardware.Camera$EventHandler.handleMessage(Camera.java:545)
02-25 08:33:43.483: E/AndroidRuntime(355):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 08:33:43.483: E/AndroidRuntime(355):  at android.os.Looper.loop(Looper.java:123)
02-25 08:33:43.483: E/AndroidRuntime(355):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-25 08:33:43.483: E/AndroidRuntime(355):  at java.lang.reflect.Method.invokeNative(Native Method)
02-25 08:33:43.483: E/AndroidRuntime(355):  at java.lang.reflect.Method.invoke(Method.java:507)
02-25 08:33:43.483: E/AndroidRuntime(355):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-25 08:33:43.483: E/AndroidRuntime(355):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-25 08:33:43.483: E/AndroidRuntime(355):  at dalvik.system.NativeStart.main(Native Method)
02-25 08:33:46.753: I/Process(355): Sending signal. PID: 355 SIG: 9

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恋你朝朝暮暮 2025-01-15 02:28:13

只是为了理解:相机本身不知道方向,它的CCD芯片
牢固地焊接到主板上,相机提供的原始图片原点位于横向放置的手机左上角,x 方向向右,y 方向向下 - 没有什么可以改变这一事实。您可以处理原始图像以更改其方向。

还要记住,当方向改变时,你的活动会被重新创建,你必须正确处理相机:

  • 停止预览,
  • 等待表面视图重新配置,
  • 再次开始预览

PS:当你的手机要睡眠/锁定屏幕时,启动器可以强制改变布局的方向,即使您禁止它。

Just for understanding: camera itself is not aware of orientation, its CCD chip
is firmly soldered to motherboard, and raw picture provided by camera has origin in top left corner of phone held in landscape position, with x direction to the right and y to the bottom - there is nothing that can change this fact. It's up to you to process raw image to change its orientation.

And also keep in mind, that when orientation changes, your ativity is recreated and you have to handle camera properly:

  • stop preview
  • wait until surface view isreconfigured
  • start preview again

PS: when your phone is going to slep/lock screen, launcher can forcibly change orientation of layout even if you prohibited it .

你穿错了嫁妆 2025-01-15 02:28:13

您在清单文件中指定 android:screenOrientation="landscape"。因此它可能仅在该方向上运行,因为 bcoz of that.dnt 指定了这一点。

Your Specifying android:screenOrientation="landscape" inur manifest file.so it may be running only in that orientation bcoz of that.dnt specify that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文