Android でカメラのプレビューを実行しようとしていますが、アプリを実行すると、タイトルと表面のビュー サイズが (0px,0px) の黒いウィンドウが表示されます。
これは私の活動です:
package rolilink.test.android;
import java.io.IOException;
import android.hardware.Camera;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
public class CameraView extends Activity{
private SurfaceView surface;
private Camera camera;
private Button stopbutton;
protected void onCreate(Bundle SavedStateInstance){
super.onCreate(SavedStateInstance);
setContentView(R.layout.cameraview);
Log.i("LogCamera", "Setteo el view");
this.getUi();
this.setListeners();
try {
this.startCameraPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
private void getUi(){
this.surface=(SurfaceView) findViewById(R.id.CameraSurface);
this.stopbutton=(Button) findViewById(R.id.btnStopPreview);
}
private void setListeners(){
this.stopbutton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
finishWithCameraPreview();
}
});
}
private void startCameraPreview() throws IOException{
this.camera= Camera.open();
Camera.Parameters parameters = camera.getParameters();
this.camera.setParameters(parameters);
SurfaceHolder holder = this.surface.getHolder();
this.camera.setPreviewDisplay(holder);
camera.startPreview();
Log.i("LogCamera", "Size of SurfaceView:"+this.surface.getHeight()+"x"+this.surface.getWidth());
}
private void finishWithCameraPreview(){
this.camera.stopPreview();
this.camera.release();
}
}
そして私のレイアウトカメラビュー:
<?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:gravity="center">
<SurfaceView
android:id="@+id/CameraSurface"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btnStopPreview"
android:text="Stop Preview"
android:layout_marginTop="2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>