Android 简单相机活动
我只是想让固定电话自动化拍照。 (我不想在 UI 中预览,只需要将图像保存到 SD 卡)。我在调用 camera.open()
的行收到“无法连接到相机服务”的消息。大多数示例都使用表面实现回调,但这是不必要的。我做错了什么?我的清单中有正确的权限:
<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"></uses-permission>
这是我正在使用的类,但显然有问题!
public class SimpleCameraActivity extends Activity {
Context mContext = this;
Button takePicture;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.setContentView(R.layout.main);
takePicture = (Button) findViewById(R.id.takePicture);
takePicture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
takePicture();
}
});
}
public void takePicture() {
Camera camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
parameters.set("camera-id", 2);
camera.setParameters(parameters);
camera.takePicture(null, rawCallback, null);
camera.release();
}
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera camera) {
if (imageData != null) {
saveImage(mContext, imageData, 100);
}
}
};
public void saveImage(Context mContext, byte[] imageData, int quality) {
FileOutputStream fileOutputStream = null;
String filePath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
filePath += "/image.jpg";
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 5;
Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,
imageData.length, options);
fileOutputStream = new FileOutputStream(filePath);
BufferedOutputStream bos = new BufferedOutputStream(
fileOutputStream);
myImage.compress(CompressFormat.JPEG, quality, bos);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I'm simply trying to automate a fixed phone to take pictures. (I don't want a preview in UI, just need the image saved to SD card). I get a "Failed to connect to camera service" at the line where I call camera.open()
. Most examples implement a callback with surfaces, but that is unneccessary. What am I doing wrong? I have the correct permission in my manifest:
<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"></uses-permission>
This is the class I'm using, but something is clearly wrong!
public class SimpleCameraActivity extends Activity {
Context mContext = this;
Button takePicture;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.setContentView(R.layout.main);
takePicture = (Button) findViewById(R.id.takePicture);
takePicture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
takePicture();
}
});
}
public void takePicture() {
Camera camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
parameters.set("camera-id", 2);
camera.setParameters(parameters);
camera.takePicture(null, rawCallback, null);
camera.release();
}
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera camera) {
if (imageData != null) {
saveImage(mContext, imageData, 100);
}
}
};
public void saveImage(Context mContext, byte[] imageData, int quality) {
FileOutputStream fileOutputStream = null;
String filePath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
filePath += "/image.jpg";
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 5;
Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,
imageData.length, options);
fileOutputStream = new FileOutputStream(filePath);
BufferedOutputStream bos = new BufferedOutputStream(
fileOutputStream);
myImage.compress(CompressFormat.JPEG, quality, bos);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不支持的 UI 中进行预览。您必须具有“UI 预览”。
不,这是非常有必要的。
That is not supported. You must have "a preview in UI".
No, it is very necessary.