我想把camera显示到PopupWindow但是怎么都不行?请大侠帮忙看看十分感谢!一下是我的代码
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.wanghai.camerapreview"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<!-- 授予程序使用摄像头的权限 -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" /> <application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:screenOrientation="landscape"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application></manifest>
MainActivity.java
package org.wanghai.camerapreview;
import android.hardware.Camera;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.PopupWindow;public class MainActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
private View viewDia;
private PopupWindow pw;
private FrameLayout preview;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCamera = getCameraInstance();
findViewById(R.id.start_popu).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewDia = LayoutInflater.from(MainActivity.this).inflate(
R.layout.pupo_main, null);
pw = new PopupWindow(MainActivity.this);
pw.setWidth(LayoutParams.MATCH_PARENT);
pw.setHeight(440);
pw.setContentView(viewDia);
pw.showAtLocation(findViewById(R.id.main), Gravity.TOP, 0, 0);
mPreview = new CameraPreview(MainActivity.this, mCamera);
preview = (FrameLayout)viewDia.findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
});
}
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open();
System.out.println(c);// attempt to get a Camera instance
System.out.println("camera open ok !");
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
}CameraPreview.java
package org.wanghai.camerapreview;import java.io.IOException;import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
static final String TAG = "CameraPreview";
private SurfaceHolder mHolder;
private Camera mCamera; public CameraPreview(Context context, Camera camera) {
super(context);
System.out.println("CameraPreview "+camera);
mCamera = camera; // Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
} public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
} public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
try {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
} public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it. if (mHolder.getSurface() == null){
// preview surface does not exist
return;
} // stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
} // set preview size and make any resize, rotate or
// reformatting changes here // start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview(); } catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}
布局文件:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main">
<Button
android:id="@+id/start_popu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="startpupo"
/></RelativeLayout>pupo_main.xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="352dp"
android:layout_height="288dp"
android:layout_weight="1" />
</RelativeLayout>哪位大侠帮忙看看!谢谢了!
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.wanghai.camerapreview"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<!-- 授予程序使用摄像头的权限 -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" /> <application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:screenOrientation="landscape"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application></manifest>
MainActivity.java
package org.wanghai.camerapreview;
import android.hardware.Camera;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.PopupWindow;public class MainActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
private View viewDia;
private PopupWindow pw;
private FrameLayout preview;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCamera = getCameraInstance();
findViewById(R.id.start_popu).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewDia = LayoutInflater.from(MainActivity.this).inflate(
R.layout.pupo_main, null);
pw = new PopupWindow(MainActivity.this);
pw.setWidth(LayoutParams.MATCH_PARENT);
pw.setHeight(440);
pw.setContentView(viewDia);
pw.showAtLocation(findViewById(R.id.main), Gravity.TOP, 0, 0);
mPreview = new CameraPreview(MainActivity.this, mCamera);
preview = (FrameLayout)viewDia.findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
});
}
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open();
System.out.println(c);// attempt to get a Camera instance
System.out.println("camera open ok !");
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
}CameraPreview.java
package org.wanghai.camerapreview;import java.io.IOException;import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
static final String TAG = "CameraPreview";
private SurfaceHolder mHolder;
private Camera mCamera; public CameraPreview(Context context, Camera camera) {
super(context);
System.out.println("CameraPreview "+camera);
mCamera = camera; // Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
} public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
} public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
try {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
} public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it. if (mHolder.getSurface() == null){
// preview surface does not exist
return;
} // stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
} // set preview size and make any resize, rotate or
// reformatting changes here // start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview(); } catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}
布局文件:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main">
<Button
android:id="@+id/start_popu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="startpupo"
/></RelativeLayout>pupo_main.xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="352dp"
android:layout_height="288dp"
android:layout_weight="1" />
</RelativeLayout>哪位大侠帮忙看看!谢谢了!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货