Код: Выделить всё
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.FrameLayout;
import android.widget.TextView;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
public class MainActivity extends Activity implements Camera.PictureCallback, SurfaceHolder.Callback {
private static final int NO_FRONT_CAMERA = -1;
private TextView myText;
private Camera mCamera;
private boolean mPreviewIsRunning = false;
private boolean mIsTakingPicture = false;
public class CameraPreview extends SurfaceView {
public CameraPreview(Context context) {
super(context);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SurfaceView surfaceView = new CameraPreview(this);
((FrameLayout) findViewById(R.id.surfaceHolder)).addView(surfaceView);
SurfaceHolder holder = surfaceView.getHolder();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.addCallback(this);
/* myText = new TextView(this);
myText.setText("Hello World");
setContentView(myText);*/
}
[b] здесь срабатывает исключение [/b]
@Override
protected void onResume() {
startPreview();
super.onResume();
}
@Override
protected void onPause() {
stopPreview();
super.onPause();
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
final int cameraId = getFrontCameraId();
if (cameraId != NO_FRONT_CAMERA) {
try {
mCamera = Camera.open(cameraId);
Camera.Parameters parameters = mCamera.getParameters();
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
parameters.setRotation(270);
List<String> flashModes = parameters.getSupportedFlashModes();
if (flashModes != null && flashModes.contains(Camera.Parameters.FLASH_MODE_OFF))
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
List<String> whiteBalance = parameters.getSupportedWhiteBalance();
if (whiteBalance != null && whiteBalance.contains(Camera.Parameters.WHITE_BALANCE_AUTO))
parameters.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
List<String> focusModes = parameters.getSupportedFocusModes();
if (focusModes != null && focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO))
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
List<Camera.Size> sizes = parameters.getSupportedPictureSizes();
if (sizes != null && sizes.size() > 0) {
Camera.Size size = sizes.get(0);
parameters.setPictureSize(size.width, size.height);
}
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
if (previewSizes != null) {
Camera.Size previewSize = previewSizes.get(previewSizes.size() - 1);
parameters.setPreviewSize(previewSize.width, previewSize.height);
}
mCamera.setParameters(parameters);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
mCamera.enableShutterSound(false);
} catch (RuntimeException e) {
//A.handleException(e, true);
finish();
return;
}
} else {
Log.e("myLogs", "Could not find front-facing camera");
finish();
return;
}
try {
mCamera.setPreviewDisplay(surfaceHolder);
} catch (IOException ioe) {
//A.handleException(ioe, true);
finish();
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {
startPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
releaseCamera();
}
@Override
public void onPictureTaken(byte[] bytes, Camera camera) {
mIsTakingPicture = false;
releaseCamera();
//noinspection PrimitiveArrayArgumentToVariableArgMethod
// new SaveImageTask().execute(bytes);
Bitmap photo = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
tempSavePhoto(photo);
finish();
}
private int getFrontCameraId() {
final int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) return i;
}
return NO_FRONT_CAMERA;
}
private void startPreview() {
if (!mPreviewIsRunning && mCamera != null) {
try {
mCamera.startPreview();
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean b, Camera camera) {
if (!mIsTakingPicture) {
try {
mIsTakingPicture = true;
mCamera.setPreviewCallback(null);
mCamera.takePicture(null, null, MainActivity.this);
} catch (RuntimeException e) {
//A.handleException(e, true);
finish();
}
}
}
});
mPreviewIsRunning = true;
} catch (Exception e) {
//A.handleException(e, true);
finish();
}
}
}
private void stopPreview() {
if (!mIsTakingPicture && mPreviewIsRunning && mCamera != null) {
mCamera.stopPreview();
mPreviewIsRunning = false;
}
}
private void releaseCamera() {
if (mCamera != null) {
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
public void tempSavePhoto(Bitmap photo){
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "test.jpg");
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FileOutputStream fo = null;
try {
fo = new FileOutputStream(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fo.write(bytes.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}
}
Код: Выделить всё
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.previewexam.previewexam" >
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
android:excludeFromRecents="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>