实现键盘输入信息,按enter键调用摄像头自动拍照

将键盘与手机通过OTG数据线连接就可实现输入信息   

通过键盘输入信息,按enter键,调用摄像头自动拍照,并保存照片

1.activity_main.xml

2.MainActivity.class

public class MainActivity extends Activity {
private String imageFilePath;
private EditText input;
private TextView output;
private ImageView imageview;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText) findViewById(R.id.input);
imageview = (ImageView) findViewById(R.id.imageview);
output = (TextView) findViewById(R.id.output);
input.setOnKeyListener(onKey);

}

OnKeyListener onKey = new OnKeyListener() {

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {

// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_ENTER) {//Enter键被按下
Toast.makeText(MainActivity.this, " onKey ", 500).show();
InputMethodManager imm =
(InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);

if(imm.isActive()){
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
}
output.setText(input.getText().toString() + "*");
Toast.makeText(MainActivity.this, output.getText().toString(),
1000).show();
startActivity(new Intent(MainActivity.this,CameraTestActivity.class));
return true;
}
return false;
}

@SuppressLint("SimpleDateFormat")
private void letCamera() {
// TODO Auto-generated method stub
imageFilePath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/mypicture.jpg";
File imageFile = new File(imageFilePath);
Uri imageFileUri = Uri.fromFile(imageFile);

Intent i = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
startActivityForResult(i, 0);
}

};

protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case 0:
// 如果拍照成功
if (resultCode == RESULT_OK) {
// 取得屏幕的显示大小
Display currentDisplay = getWindowManager().getDefaultDisplay();
int dw = currentDisplay.getWidth();
int dh = currentDisplay.getHeight();
// 对拍出的照片进行缩放
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(imageFilePath,
bmpFactoryOptions);
int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight
/ (float) dh);
int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth
/ (float) dw);
if (heightRatio > 1 && widthRatio > 1) {
if (heightRatio > widthRatio) {
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
bmpFactoryOptions.inJustDecodeBounds = false;
bmp = BitmapFactory
.decodeFile(imageFilePath, bmpFactoryOptions);
imageview.setImageBitmap(bmp);
}
break;
default:
break;
}

};

}

3.CameraTestActivity.class

public class CameraTestActivity extends Activity{
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);// 设置横屏模式以及全屏模式
setContentView(new CameraView(this)); //设置View
}
}

4.CameraView.class  调用摄像头,自动拍照


public class CameraView extends SurfaceView implements SurfaceHolder.Callback,
Camera.PictureCallback {
private SurfaceHolder holder;
private Camera camera;
private boolean af;

@SuppressWarnings("deprecation")
public CameraView(Context context) {// 构造函数
super(context);

holder = getHolder();// 生成Surface Holder
holder.addCallback(this);

holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);// 指定Push Buffer
}

public void surfaceCreated(SurfaceHolder holder) {// Surface生成事件的处理
try {
camera = Camera.open();// 摄像头的初始化
camera.setPreviewDisplay(holder);
} catch (Exception e) {
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {// Surface改变事件的处理
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(width, height);
camera.setParameters(parameters);// 设置参数
camera.startPreview();// 开始预览
// 自动聚焦拍照
takePhotos();
}
private boolean takePhotos() {
// TODO Auto-generated method stub
camera.autoFocus(null);
camera.takePicture(null, null, this);
return true;
}

public void surfaceDestroyed(SurfaceHolder holder) {// Surface销毁时的处理
camera.setPreviewCallback(null);
camera.stopPreview();
camera.release();
camera = null;

}

public void onPictureTaken(byte[] data, Camera camera) {// 拍摄完成后保存照片
try {
String path = Environment.getExternalStorageDirectory()
+ "/test"+999+".jpg";
Toast.makeText(getContext(), path, 1000).show();
data2file(data, path);
} catch (Exception e) {
}
camera.startPreview();
// takePhotos();
}
private void data2file(byte[] w, String fileName) throws Exception {// 将二进制数据转换为文件的函数
FileOutputStream out = null;
try {
out = new FileOutputStream(fileName);
out.write(w);
out.flush();
out.close();
} catch (Exception e) {
if (out != null)
out.close();
throw e;
}
}

}

5.权限

<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

原文地址:https://www.cnblogs.com/xiaoshumiao/p/7307234.html