我只想尝试自动固定手机拍照。 (我不想在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();
}
}
}
答案 0 :(得分:4)
我不希望在UI中进行预览
不支持。您必须具有“UI预览”。
大多数示例都使用曲面实现回调,但这是不必要的。
不,这是非常必要的。