Back to project page gatoloco.
The source code is released under:
GNU General Public License
If you think the Android project gatoloco listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.grumpycat; //www. j av a 2 s. c o m import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import android.content.Context; import android.content.res.Configuration; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.hardware.Camera; import android.hardware.Camera.PreviewCallback; import android.os.Environment; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; class Preview extends SurfaceView implements SurfaceHolder.Callback { private static final String TAG = "Preview"; SurfaceHolder mHolder; public Camera camera; public String filename; Preview(Context context) { super(context); // Install a SurfaceHolder.Callback so we get notified when the // underlying surface is created and destroyed. 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.Parameters parameters = camera.getParameters(); if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) { parameters.set("orientation", "portrait"); // For Android Version 2.2 and above camera.setDisplayOrientation(90); // For Android Version 2.0 and above parameters.setRotation(90); } // camera.setPreviewCallback(new PreviewCallback() { // // public void onPreviewFrame(byte[] data, Camera arg1) { // FileOutputStream outStream = null; // try { // filename = String.format(Environment // .getExternalStorageDirectory().getPath() // + "/%d.jpg", System.currentTimeMillis()); // outStream = new FileOutputStream(filename); // outStream.write(data); // outStream.close(); // Log.d(TAG, "onPreviewFrame - wrote bytes: " // + data.length); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } catch (IOException e) { // e.printStackTrace(); // } finally { // } // Preview.this.invalidate(); // } // }); } catch (IOException e) { e.printStackTrace(); } } public void surfaceDestroyed(SurfaceHolder holder) { camera.stopPreview(); camera.release(); // camera = null; } public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // Camera.Parameters parameters = camera.getParameters(); // List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes(); // Camera.Size previewSize = previewSizes.get(0); // parameters.setPreviewSize(previewSize.width, previewSize.height); // parameters.getHorizontalViewAngle(); // // camera.setParameters(parameters); camera.startPreview(); } @Override public void draw(Canvas canvas) { super.draw(canvas); Paint p = new Paint(Color.RED); Log.d(TAG, "draw"); canvas.drawText("PREVIEW", canvas.getWidth() / 2, canvas.getHeight() / 2, p); } public int changeCamera() { return 1; } }