AndroidでQRコードを読むアプリをつくろうと思ったんだけど、まずはカメラのプレビューを表示できた
職人の朝は早い。
前回。
AndroidでQRコードを読むアプリをつくろうと思ったんだけど、そもそもカメラのプレビューを表示したことなかったからやってみたんだけど、結構エグかった - マサルの備忘録
クソコードはこちら
プレビューは横長と仮定した実装。
Activity拡張クラスに全部ベタ書きしてます。
SurfaceViewのサイズ変更はゴリゴリやってます。心の目で見てください。
はいはいdeprecatedですよ
こんなのばっかり。
package com.masaru.qrcodereader; import java.io.IOException; import java.util.List; import android.app.Activity; import android.hardware.Camera; import android.hardware.Camera.CameraInfo; import android.hardware.Camera.Size; import android.os.Bundle; import android.util.Log; import android.view.Display; import android.view.Menu; import android.view.MenuItem; import android.view.Surface; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; import android.widget.RelativeLayout; import android.widget.TextView; public class MainActivity extends Activity { private RelativeLayout mRelativeLayout; private SurfaceView mSurfaceView; private TextView mTextView; private Camera mCamera; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onResume() { super.onResume(); mTextView = (TextView) findViewById(R.id.result_textview); mSurfaceView = (SurfaceView) findViewById(R.id.surface_view); mRelativeLayout = (RelativeLayout) findViewById(R.id.relative_layout); mSurfaceView.getHolder().addCallback(mCallback); } private void setParameters() { Camera.Parameters parameters = mCamera.getParameters(); List<Camera.Size> sizes = parameters.getSupportedPreviewSizes(); Camera.Size size = sizes.get(0); parameters.setPreviewSize(size.width, size.height); mCamera.setParameters(parameters); mTextView.setText(size.width + " x " + size.height); } private void setDisplayOrientation() { int degree; switch (getWindowManager().getDefaultDisplay().getRotation()) { case Surface.ROTATION_0: degree = 0; break; case Surface.ROTATION_90: degree = 90; break; case Surface.ROTATION_180: degree = 180; break; case Surface.ROTATION_270: degree = 270; break; default: degree = 0; } CameraInfo info = new CameraInfo(); Camera.getCameraInfo(0, info); mCamera.setDisplayOrientation((info.orientation - degree + 360) % 360); } private void setSurfaceViewSize() { float relativeWidth = mRelativeLayout.getWidth(); float relativeHeight = mRelativeLayout.getHeight(); float previewWidth; float previewHeight; Size size = mCamera.getParameters().getSupportedPreviewSizes().get(0); if (relativeHeight > relativeWidth) { previewHeight = size.width; previewWidth = size.height; } else { previewHeight = size.height; previewWidth = size.width; } int surfaceWidth; int surfaceHeight; if (previewHeight / relativeHeight > previewWidth / relativeWidth) { surfaceWidth = (int) (previewWidth * relativeHeight / previewHeight); surfaceHeight = (int) (relativeHeight); } else { surfaceWidth = (int) (relativeWidth); surfaceHeight = (int) (previewHeight * relativeWidth / previewWidth); } RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( surfaceWidth, surfaceHeight); params.addRule(RelativeLayout.CENTER_IN_PARENT); mSurfaceView.setLayoutParams(params); } private SurfaceHolder.Callback mCallback = new Callback() { @Override public void surfaceDestroyed(SurfaceHolder holder) { mCamera.release(); mCamera = null; } @Override public void surfaceCreated(SurfaceHolder holder) { mCamera = Camera.open(); setParameters(); setDisplayOrientation(); setSurfaceViewSize(); try { mCamera.setPreviewDisplay(holder); } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { mCamera.startPreview(); } }; }