OK 私はこのカメラ コードを持っています。すべての向きで動作するようにしたいのです。およびアンドロイド 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
しかし、私は運がありません。私がしようとすることはすべて失敗します。助けてください。
アクティビティ.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