已阅读书籍和提示,摆脱所有编译错误和警告,并输入一些调试声明。
package com.cit.BroadcastSim;
import java.io.IOException;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class BroadcastActivity extends Activity implements SurfaceHolder.Callback {
public Camera myCamera;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.broadcast); // Inflate the broadcast.xml file
Log.d("BROADCAST", "Creating the Activity");
SurfaceView cameraSurface = (SurfaceView)findViewById(R.id.camerasurface);
SurfaceHolder cameraHolder = cameraSurface.getHolder();
cameraHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
cameraHolder.addCallback(this);
Log.d("BROADCAST", "Now wait for some CallBacks");
}
public void surfaceCreated(SurfaceHolder holder) {
// Surface created, now it is possible to set the preview
Log.d("BROADCAST", "Surface Created");
try {
Log.d("BROADCAST","CAMERA: NOT NULL");
myCamera = Camera.open();
myCamera.setPreviewDisplay(holder);
myCamera.startPreview();
} catch (IOException e) {
Log.d("BROADCAST", e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d("BROADCAST", "Surface Destroyed");
myCamera.stopPreview();
myCamera.release();
}
public void surfaceChanged(SurfaceHolder holder, int I, int J, int K) {
Log.d("BROADCAST", "Surface Changed");
myCamera.stopPreview();
myCamera.release();
}
}
在DDMS调试器中,我得到一条“创建活动”的日志消息,然后是“现在等待一些回调”,而我的调试消息就没那么了,所以我认为回调不起作用 - 对于我的生活看不出我错了。
在清单中我有
活动XML页面有
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Broadcast"
/>
<SurfaceView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/camerasurface"
/>
</LinearLayout>
最后,在Android手机(HTC Wildfire)上加载页面,textView消息显示在左上角,这就是全部。
应该提到我对这个平台很新,并接受我可能错过了非常基本的东西。
非常感谢任何想法/评论,
奥利弗
答案 0 :(得分:1)
查看ApiDemos(Graphics-&gt; CameraPreview)和CameraPreview类。这就是我所做的(所有的ApiDemos都很棒),它就像一个魅力。先让一些东西工作是件好事 - 然后你就可以把它从你应用中不需要的东西上剥下来。该演示也在线:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html。希望有所帮助。
答案 1 :(得分:0)
没有足够的时间来解释(在几分钟内与朋友会面)。我基本上将一些函数添加到“surfaceChanged()”中(这是你应该开始预览的地方)。
因此你不需要“surfaceCreated()”中的mCam.startPreview()
public void surfaceCreated(SurfaceHolder holder) {
// Surface created, now it is possible to set the preview
Log.d("BROADCAST", "Surface Created");
try {
Log.d("BROADCAST","CAMERA: NOT NULL");
mCam = Camera.open();
mCam.setPreviewDisplay(holder);
//myCamera.startPreview();
} catch (IOException e) {
Log.d("BROADCAST", e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d("BROADCAST", "Surface Destroyed");
mCam.stopPreview();
mCam.release();
}
public void surfaceChanged(SurfaceHolder holder, int I, int J, int K) {
Camera.Parameters parameters = mCam.getParameters();
List<Size> previewSizes = parameters.getSupportedPreviewSizes();
Size bestSize = null;
int bestDiff = 0;
int diff = 0;
for (Size size : previewSizes) {
diff = Math.abs(K - size.height) + Math.abs(J - size.width);
if (bestSize == null || diff < bestDiff) {
bestSize = size;
bestDiff = diff;
}
parameters.setPreviewSize(bestSize.width, bestSize.height);
mCam.setParameters(parameters);
}
//start preview of camera
mCam.startPreview();
}
xml-file(你做了一点复制和粘贴错误)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
希望我能帮忙:) 我认为,通过设置方向,您可以将相机图片更改为纵向模式。 :)(现在是风景)
答案 2 :(得分:0)
老实说,我认为整个相机预览处理“手动”过于复杂。 如果您只想拍照并预先进行预览,可以通过Intent告诉系统。我前段时间写了一篇关于此事的帖子http://javablogs.com/Jump.action?id=618025 基本上它是:
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
然后在您的活动中,您可以在onActivityResult()
回调
@Override
public void onActivityResult(int requestCode, int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1&& resultCode==RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
}
}
如果您需要比默认图像更大的图像,请查看MediaStore.EXTRA_OUTPUT。