我一直在搜索相机应用程序的一些代码并得到一个但是当我完成代码时它给了我一些错误。代码任何一个请纠正我在代码中的错误。
我的应用程序代码如下
package abc.bae.camer;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.graphics.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
public class Bharath_cameraActivity extends Activity {
private static final String TAG = "Bharath_cameraActivity";
public Camera camera;
public Preview preview;
public Button buttonClick;
public FrameLayout fl;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
preview = new Preview(this);
fl =(FrameLayout)findViewById(R.id.preview).addView(preview);
buttonClick = (Button) findViewById(R.id.click);
buttonClick.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
preview.camera.takePicture(null,null, null);
}
});
}
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
}
};
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, android.hardware.Camera camera) {
// TODO Auto-generated method stub
}
};
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(String.format(
"/sdcard/%d.jpg", System.currentTimeMillis()));
outStream.write(data);
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void onPictureTaken(byte[] data, android.hardware.Camera camera) {
// TODO Auto-generated method stub
}
};
}
Preview.java的代码是
package abc.bae.camer;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Camera.PreviewCallback;
import android.view.SurfaceHolder;
class Preview extends Bharath_cameraActivity implements SurfaceHolder.Callback {
private static final String TAG = "Preview";
SurfaceHolder mHolder;
public Camera camera;
Preview(Context context) {
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
camera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera arg1) {
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(String.format(
"/sdcard/%d.jpg", System.currentTimeMillis()));
outStream.write(data);
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Preview.this.invalidate();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(w, h);
camera.setParameters(parameters);
camera.startPreview();
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
Paint p = new Paint(Color.RED);
canvas.drawText("PREVIEW", canvas.getWidth() / 2,
canvas.getHeight() / 2, p);
}
}
答案 0 :(得分:0)
这里是我的简单照片代码希望对此有所帮助
private static final int TAKE_PHOTO_CODE = 1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
takePhoto();
}
...
private void takePhoto(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile()));
startActivityForResult(intent,TAKE_PHOTO_CODE);
}
...
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK){
// do stub like crop, rename , rotate pictue
}
else finish();
}
...
private File getTempFile(){
File root = new File(Environment.getExternalStorageDirectory(),"picture");
if(!root.exists()){
root.mkdirs();
}
File file = new File(root,"picture.jpg");
return file;
}
答案 1 :(得分:0)
我有一些代码,它可以帮到你:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1888);
并且您还必须覆盖onActivityResult()
方法才能完成该过程
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1888){
if (resultCode == Activity.RESULT_OK) {
bitmap = (Bitmap) data.getExtras().get("data");
//show the bitmapImage
}
}
}
}