次のコードを使用して画像をキャプチャします。その中で私は使用しますSurfaceview
。キャプチャした画像は、Android Sdcard で作成したフォルダーに保存されますが、これらの画像を画像の向きで表示するGridView
と、画像の向きが異なります。カメラを使用するとIntent (ACTION_IMAGE_CAPTURE)
、出力画像が明確に定義されます。誰でも私を助けることができます
ここに Activity を実装するクラスがありますSurfaceHolder.Callback
public class CameraView extends Activity implements SurfaceHolder.Callback {
private static final String TAG = "CameraVeiw";
private Camera camera;
boolean previewing = false;
SurfaceHolder surfaceHolder;
private File file = null;
private static String mFileName = null;
private long name;
final int RESULT_SAVEIMAGE = 0;
private int w;
private int h;
public static byte[] data = null;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
}
私はSurfaceView
画像をキャプチャするために使用します
SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceView.getWindowVisibility();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceHolder.setFixedSize(300, 300); //hard coded
Button button2 = (Button) findViewById(R.id.picbutton);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
camera.takePicture(new CustomShutter(), null,
new CustomPictureCallback());
}
});
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if(previewing){
camera.stopPreview();
previewing = false;
}
if (camera != null){
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
camera.setDisplayOrientation(90); }
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
class CustomPictureCallback implements Camera.PictureCallback {
private Bitmap bitmap;
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
String path = "/sdcard/MonsterApp/Photos/" + data+".jpg";
outStream = new FileOutputStream(String.format(path));
outStream.write(data);
outStream.close();
Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d(TAG, "onPictureTaken - jpeg");
camera.startPreview();
Log.i("Information", "Helllllooooo"); } }
class CustomShutter implements Camera.ShutterCallback {
public void onShutter() {
}
}
@Override
public void onDestroy() {
super.onDestroy();
if(camera != null) {
camera.release();
}
}
}