android – 如何从相机保存图像?

android – 如何从相机保存图像?,第1张

概述这是我的代码: package com.commonsware.android.skeleton;import android.app.Activity;import android.content.Context;import android.hardware.Camera;import android.hardware.Camera.*;import android.os.Bund 这是我的代码:

package com.commonsware.androID.skeleton;import androID.app.Activity;import androID.content.Context;import androID.harDWare.Camera;import androID.harDWare.Camera.*;import androID.os.Bundle;import androID.os.Environment;import androID.util.Log;import androID.vIEw.display;import androID.vIEw.Surface;import androID.vIEw.SurfaceHolder;import androID.vIEw.SurfaceVIEw;import androID.vIEw.Window;import androID.vIEw.WindowManager;import androID.Widget.FrameLayout;import java.io.fileNotFoundException;import java.io.fileOutputStream;import java.io.IOException;import java.util.List;// ----------------------------------------------------------------------public class SimpleBulbActivity extends Activity {    private PrevIEw mPrevIEw;    private static final String TAG = "CameraDemo";    FrameLayout prevIEw;    Camera mCamera;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // HIDe the window Title.        requestwindowFeature(Window.FEATURE_NO_Title);        setContentVIEw(R.layout.main);    }    protected voID onResume() {        super.onResume();        //Setup the FrameLayout with the Camera PrevIEw Screen        mPrevIEw = new PrevIEw(this);        prevIEw = (FrameLayout)findVIEwByID(R.ID.prevIEw);         prevIEw.addVIEw(mPrevIEw);    }    public voID snap() {        mCamera.takePicture(shutterCallback,rawCallback,jpegCallback);    }    ShutterCallback shutterCallback = new ShutterCallback() {      public voID onShutter() {          Log.d(TAG,"onShutter'd");      }    };    PictureCallback rawCallback = new PictureCallback() {      public voID onPictureTaken(byte[] _data,Camera _camera) {          Log.d(TAG,"onPictureTaken - raw");      }    };    PictureCallback jpegCallback = new PictureCallback() {      public voID onPictureTaken(byte[] data,Camera _camera) {          fileOutputStream outStream = null;            try {                // write to local sandBox file system                // outStream =                // CameraDemo.this.openfileOutput(String.format("%d.jpg",// System.currentTimeMillis()),0);                // Or write to sdcard                outStream = new fileOutputStream(String.format(                        "/sdcard/%d.jpg",System.currentTimeMillis()));                outStream.write(data);                outStream.close();                Log.d(TAG,"onPictureTaken - wrote bytes: " + data.length);            } catch (fileNotFoundException e) {                e.printstacktrace();            } catch (IOException e) {                e.printstacktrace();            } finally {            }            Log.d(TAG,"onPictureTaken - jpeg");      }    }; // ----------------------------------------------------------------------    class PrevIEw extends SurfaceVIEw implements SurfaceHolder.Callback {        SurfaceHolder mHolder;        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) {            // The Surface has been created,acquire the camera and tell it where            // to draw.            mCamera = Camera.open();            try {               mCamera.setPrevIEwdisplay(holder);               mCamera.setPrevIEwCallback(new PrevIEwCallback() {                public voID onPrevIEwFrame(byte[] data,Camera arg1) {                    fileOutputStream outStream = null;                    try {                        outStream = new fileOutputStream(Environment.getExternalStorageDirectory().toString());                        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) {                mCamera.release();                mCamera = null;                e.printstacktrace();            }        }        public voID surfaceDestroyed(SurfaceHolder holder) {            // Surface will be destroyed when we return,so stop the prevIEw.            // Because the CameraDevice object is not a shared resource,it's very            // important to release it when the activity is paused.            mCamera.stopPrevIEw();            mCamera.release();            mCamera = null;        }        private Size getoptimalPrevIEwSize(List<Size> sizes,int w,int h) {            final double ASPECT_TolERANCE = 0.05;            double targetRatio = (double) w / h;            if (sizes == null) return null;            Size optimalSize = null;            double minDiff = Double.MAX_VALUE;            int targetHeight = h;            // Try to find an size match aspect ratio and size            for (Size size : sizes) {                double ratio = (double) size.wIDth / size.height;                if (Math.abs(ratio - targetRatio) > ASPECT_TolERANCE) continue;                if (Math.abs(size.height - targetHeight) < minDiff) {                    optimalSize = size;                    minDiff = Math.abs(size.height - targetHeight);                }            }            // Cannot find the one match the aspect ratio,ignore the requirement            if (optimalSize == null) {                minDiff = Double.MAX_VALUE;                for (Size size : sizes) {                    if (Math.abs(size.height - targetHeight) < minDiff) {                        optimalSize = size;                        minDiff = Math.abs(size.height - targetHeight);                    }                }            }            return optimalSize;        }        public voID surfaceChanged(SurfaceHolder holder,int format,int h) {            // Now that the size is kNown,set up the camera parameters and begin            // the prevIEw.            Camera.Parameters parameters = mCamera.getParameters();            List<Size> sizes = parameters.getSupportedPrevIEwSizes();            Size optimalSize = getoptimalPrevIEwSize(sizes,w,h);            display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultdisplay();            if(display.getRotation() == Surface.ROTATION_0)            {                parameters.setPrevIEwSize(optimalSize.height,optimalSize.wIDth);                                           mCamera.setdisplayOrIEntation(90);            }            if(display.getRotation() == Surface.ROTATION_90)            {                parameters.setPrevIEwSize(optimalSize.wIDth,optimalSize.height);                                     }            if(display.getRotation() == Surface.ROTATION_180)            {                parameters.setPrevIEwSize(optimalSize.wIDth,optimalSize.height);                           }            if(display.getRotation() == Surface.ROTATION_270)            {                parameters.setPrevIEwSize(optimalSize.wIDth,optimalSize.height);                mCamera.setdisplayOrIEntation(0);            }            mCamera.setParameters(parameters);            mCamera.startPrevIEw();        }    }}

好的,我已经修改了一下我的代码.

我的主要布局中有这个:

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:orIEntation="vertical" androID:layout_wIDth="fill_parent"    androID:layout_height="fill_parent" androID:ID="@+ID/layout">    <TextVIEw androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content" androID:text="Camera Demo"        androID:textSize="24sp" />    <FrameLayout androID:layout_weight="1" androID:layout_wIDth="fill_parent"        androID:layout_height="fill_parent">    <FrameLayout androID:ID="@+ID/prevIEw"        androID:layout_weight="1" androID:layout_wIDth="fill_parent"        androID:layout_height="fill_parent">    </FrameLayout>    <ImageVIEw androID:src="@drawable/litbulb"                   androID:layout_wIDth="match_parent"                   androID:layout_height="112dip" />    </FrameLayout>    <button androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content" androID:ID="@+ID/buttonClick"        androID:text="Snap!" androID:layout_gravity="center"></button></linearLayout>

当我点击“Snap!”或按钮单击按钮,它应该捕获并保存图像,但事实并非如此.谁能帮我修改这段代码呢?

此外,每次离开应用程序时都会崩溃.这是相关的logcat数据:

12-21 13:30:47.820: ERROR/AndroIDRuntime(3906): FATAL EXCEPTION: main12-21 13:30:47.820: ERROR/AndroIDRuntime(3906): java.lang.RuntimeException: Method called after release()12-21 13:30:47.820: ERROR/AndroIDRuntime(3906):     at androID.harDWare.Camera.setHasPrevIEwCallback(Native Method)12-21 13:30:47.820: ERROR/AndroIDRuntime(3906):     at androID.harDWare.Camera.access0(Camera.java:114)12-21 13:30:47.820: ERROR/AndroIDRuntime(3906):     at androID.harDWare.Camera$EventHandler.handleMessage(Camera.java:519)12-21 13:30:47.820: ERROR/AndroIDRuntime(3906):     at androID.os.Handler.dispatchMessage(Handler.java:99)12-21 13:30:47.820: ERROR/AndroIDRuntime(3906):     at androID.os.Looper.loop(Looper.java:123)12-21 13:30:47.820: ERROR/AndroIDRuntime(3906):     at androID.app.ActivityThread.main(ActivityThread.java:4627)12-21 13:30:47.820: ERROR/AndroIDRuntime(3906):     at java.lang.reflect.Method.invokeNative(Native Method)12-21 13:30:47.820: ERROR/AndroIDRuntime(3906):     at java.lang.reflect.Method.invoke(Method.java:521)12-21 13:30:47.820: ERROR/AndroIDRuntime(3906):     at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)12-21 13:30:47.820: ERROR/AndroIDRuntime(3906):     at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:616)12-21 13:30:47.820: ERROR/AndroIDRuntime(3906):     at dalvik.system.NativeStart.main(Native Method)
解决方法
pre.camera.takePicture(shutterCallback,jpegCallback);            PictureCallback rawCallback = new PictureCallback() {            public voID onPictureTaken(byte[] data,Camera camera) {                System.out.println( "onPictureTaken - raw");            }        };        /** Handles data for jpeg picture */        PictureCallback jpegCallback = new PictureCallback() {            public voID onPictureTaken(byte[] data,Camera camera) {                 BitmapFactory.Options options=new BitmapFactory.Options();                    options.inSampleSize = 5;                m=BitmapFactory.decodeByteArray(data,data.length,options);
总结

以上是内存溢出为你收集整理的android – 如何从相机保存图像?全部内容,希望文章能够帮你解决android – 如何从相机保存图像?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1128924.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存