android自定義相機(jī)拍照類0001_第1頁
android自定義相機(jī)拍照類0001_第2頁
android自定義相機(jī)拍照類0001_第3頁
android自定義相機(jī)拍照類0001_第4頁
android自定義相機(jī)拍照類0001_第5頁
免費(fèi)預(yù)覽已結(jié)束,剩余1頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、importjava.io.BufferedOutputStream;importjava.io.File;import java.io.FileOutputStream;importandroid.app.Activity;importandroid.content.Intent;importandroid.graphics.Bitmap;importandroid.graphics.BitmapFactory;importandroid.graphics.Matrix;importandroid.os.Bundle;importandroid.view.View;importandroid

2、.view.View.OnClickListener;importandroid.widget.Button;importandroid.widget.ImageView;public class Main extends Activity implements OnClickListenerpublicImageViewimageView;Overridepublic void onCreate(Bundle savedInstanceState)super.onCreate(savedInstanceState); setContentView(R.layout.main);(Button

3、)Button btnTakePicture = findViewById(R.id.btnTakePicture);btnTakePicture.setOnClickListener(this);imageView = (ImageView) findViewById(R.id.imageview);OverrideintresultCode,protected void onActivityResult(intrequestCode,Intent data)if (requestCode = 1)if (resultCode = 20)System.out.println(c+System

4、.currentTimeMillis();Bitmap cameraBitmap;byte bytes = data.getExtras().getByteArray(bytes); cameraBitmap = BitmapFactory.decodeByteArray(bytes,bytes.length);0,Matrix matrix = new Matrix();matrix.setRotate(CameraPreview.getPreviewDegree(this);cameraBitmap = Bitmap.createBitmap(cameraBitmap, 0,0,camer

5、aBitmap.getWidth(),cameraBitmap.getHeight(),newmatrix, true);File myCaptureFileFile(/sdcard/+System.currentTimeMillis()+.jpg);trynewBufferedOutputStreambosBufferedOutputStream(newFileOutputStream(myCaptureFile);100,cameraBpress(Bitmap.CompressFormat.JPEG, bos);bos.flush();bos.close();imageView.setIm

6、ageBitmap(cameraBitmap);System.out.println(-);catch (Exception e)super.onActivityResult(requestCode, resultCode, data);Overridepublic void onClick(View view)Intent intent = new Intent(this, CameraPreview.class); startActivityForResult(intent, 1);packagemobile.android.custom.camera;importjava.io.IOEx

7、ception;importandroid.app.Activity;importandroid.content.Context;importandroid.graphics.PixelFormat;importandroid.hardware.Camera; importandroid.hardware.Camera.AutoFocusCallback; importandroid.hardware.Camera.PictureCallback; importandroid.os.Bundle;importandroid.util.AttributeSet;importandroid.vie

8、w.MotionEvent;importandroid.view.Surface;importandroid.view.SurfaceHolder;importandroid.view.SurfaceView; importandroid.view.ViewGroup.LayoutParams; importandroid.view.Window;importandroid.widget.ImageView;importandroid.widget.ImageView .ScaleType;public class CameraPreview extends Activityprivate P

9、review preview;privateImageViewivFocus;Overridepublic void onCreate(Bundle savedInstanceState)super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); preview = new Preview(this); setContentView(preview);ivFocus = new ImageView(this);OverridepublicbooleanonTouchEvent(Motion

10、Event event)if (event.getAction() = MotionEvent.ACTION_DOWN) preview.takePicture();System.out.println(a+System.currentTimeMillis(); returnsuper.onTouchEvent(event);public static intgetPreviewDegree(Activity activity)int rotation = activity.getWindowManager().getDefaultDisplay() .getRotation();int de

11、gree = 0;switch (rotation)case Surface.ROTATION_0: degree = 90; break;case Surface.ROTATION_90: degree = 0; break;case Surface.ROTATION_180: degree = 270; break;case Surface.ROTATION_270: degree = 180; break;return degree;class Preview extends SurfaceView implements SurfaceHolder.Callback privateSur

12、faceHolder holder; private Camera camera;public Preview(Context context, AttributeSetattrs)super(context, attrs);/ TODO Auto-generated constructor stubprivatePictureCallbackpictureCallback = new PictureCallback() Override public void onPictureTaken(byte data, Camera camera) System.out.println(b+Syst

13、em.currentTimeMillis(); getIntent().putExtra(bytes, data); setResult(20, getIntent();camera.stopPreview(); camera = null;finish();public Preview(Context context) super(context);holder = getHolder();holder.addCallback(this);holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);public void surfaceCr

14、eated(SurfaceHolder holder) camera = Camera.open();trycamera.setPreviewDisplay(holder);catch (IOException exception)camera.release(); camera = null;public void surfaceDestroyed(SurfaceHolder holder)camera.release();public void surfaceChanged(final SurfaceHolder holder, int format, int w, int h)tryCa

15、mera.Parameters parameters = camera.getParameters();parameters.setPictureFormat(PixelFormat.JPEG);parameters.setPreviewSize(w, h); parameters.setPictureSize(w , h);camera.setParameters(parameters);camera.setDisplayOrientation(getPreviewDegree(CameraPreview.this);camera.startPreview();ivFocus.setImageResource(R.drawable.focus1);LayoutParamslayoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); ivFocus.setScaleType(ScaleType.CENTER); addContentView(ivFocus, layoutParams); ivFocus.setVisibility(VISIBLE);camera.autoFocus(new Aut

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論