用相机拍照,得到位图 [英] Take a picture with camera and get bitmap

查看:163
本文介绍了用相机拍照,得到位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不是重复的:我要求为这个功能!!!!没有用户交互!!!!



我正在使用一个应用程序的android是使用前置摄像头为用户模拟镜子。我需要应用程序每5秒拍摄一张位图自动!没有用户交互!!!,我喜欢与另一个位图后面结合。





我尝试了几种解决方案,但是没有一种解决方案。

解决方案

我使用以下代码在布局背景中显示实时相机Feed,按钮将图像保存为jpeg。尝试并修改它,如你所愿:
您可以下载整个测试项目在这里,使您可以快速测试:
http://www.4shared.com/rar/v-ZQPybcce/Test.html



--- >>这段代码与其他人使用意图的区别是,它需要的图片自动无需打开相机应用程序,这使应用程序看起来更好:)< / />

  package com.mreprogramming.test; 

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;


public class CameraActivity extends Activity实现SurfaceHolder.Callback {

protected static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;
private SurfaceView SurView;
private SurfaceHolder camHolder;
private boolean previewRunning;
private button button1;
final上下文上下文= this;
public static Camera camera = null;
private ImageView camera_image;
private位图bmp,bmp1;
private ByteArrayOutputStream bos;
private BitmapFactory.Options选项,o,o2;
private FileInputStream fis;
ByteArrayInputStream fis2;
private FileOutputStream fos;
private文件dir_image2,dir_image;
private RelativeLayout CamView;

@SuppressWarnings(deprecation)
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.camera)



CamView =(RelativeLayout)findViewById(R.id.camview);

SurView =(SurfaceView)findViewById(R.id.sview);
camHolder = SurView.getHolder();
CAMHolder.addCallback(this);
camHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
button1 =(Button)findViewById(R.id.button_1);


camera_image =(ImageView)findViewById(R.id.camera_image);


button1.setOnClickListener(new OnClickListener()
{

public void onClick(View v)
{

button1.setClickable(false);
button1.setVisibility(View.INVISIBLE); //< ----- HIDE HERE
camera.takePicture(null,null,mPicture);

}

});


}




@Override
public void surfaceChanged(SurfaceHolder holder,int format,int width,
int height){
if(previewRunning){
camera.stopPreview();
}
Camera.Parameters camParams = camera.getParameters();
Camera.Size size = camParams.getSupportedPreviewSizes()。get(0);
camParams.setPreviewSize(size.width,size.height);
camera.setParameters(camParams);
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
previewRunning = true;
} catch(IOException e){
e.printStackTrace();
}
}

public void surfaceCreated(SurfaceHolder holder){
try {
camera = Camera.open();
} catch(Exception e){
e.printStackTrace();
Toast.makeText(getApplicationContext(),Error,Toast.LENGTH_LONG).show();
finish();
}
}

@Override
public void surfaceDestroyed(SurfaceHolder holder){
camera.stopPreview();
camera.release();
camera = null;
}



public void TakeScreenshot(){

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
int nu = preferences.getInt(image_num,0);
nu ++;
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(image_num,nu);
editor.commit();
CamView.setDrawingCacheEnable(true);
CamView.buildDrawingCache(true);
bmp = Bitmap.createBitmap(CamView.getDrawingCache());
CamView.setDrawingCacheEnabled(false);
bos = new ByteArrayOutputStream();
bmp.compress(CompressFormat.JPEG,100,bos);
byte [] bitmapdata = bos.toByteArray();
fis2 = new ByteArrayInputStream(bitmapdata);

String picId = String.valueOf(nu);
String myfile =MyImage+ picId +。jpeg;

dir_image = new File(Environment.getExternalStorageDirectory()+
File.separator +我的自定义文件夹);
dir_image.mkdirs();

try {
File tmpFile = new File(dir_image,myfile);
fos = new FileOutputStream(tmpFile);

byte [] buf = new byte [1024];
int len;
while((len = fis2.read(buf))> 0){
fos.write(buf,0,len);
}
fis2.close();
fos.close();

Toast.makeText(getApplicationContext(),
文件保存在:/ My Custom Folder /+MyImage+ picId +。jpeg,Toast.LENGTH_LONG).show ();

bmp1 = null;
camera_image.setImageBitmap(bmp1);
camera.startPreview();
button1.setClickable(true);
button1.setVisibility(View.VISIBLE); //< ---- UNHIDE HER
} catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}


}

private PictureCallback mPicture = new PictureCallback(){

@Override
void onPictureTaken(byte [] data,Camera camera){
dir_image2 = new File(Environment.getExternalStorageDirectory()+
File.separator +My Custom Folder);
dir_image2.mkdirs();


文件tmpFile = new File(dir_image2,TempImage.jpg);
try {
fos = new FileOutputStream(tmpFile);
fos.write(data);
fos.close();
} catch(FileNotFoundException e){
Toast.makeText(getApplicationContext(),Error,Toast.LENGTH_LONG).show();
} catch(IOException e){
Toast.makeText(getApplicationContext(),Error,Toast.LENGTH_LONG).show();
}
options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;

bmp1 = decodeFile(tmpFile);
bmp = Bitmap.createScaledBitmap(bmp1,CamView.getWidth(),CamView.getHeight(),true);
camera_image.setImageBitmap(bmp);
tmpFile.delete();
TakeScreenshot();

}
};


public位图decodeFile(文件f){
位图b = null;
try {
//解码图片大小
o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;

fis = new FileInputStream(f);
BitmapFactory.decodeStream(fis,null,o);
fis.close();
int IMAGE_MAX_SIZE = 1000;
int scale = 1;
if(o.outHeight> IMAGE_MAX_SIZE || o.outWidth> IMAGE_MAX_SIZE){
scale =(int)Math.pow(
2,
(int) round(Math.log(IMAGE_MAX_SIZE
/(double)Math.max(o.outHeight,o.outWidth))
/ Math.log(0.5)));
}

//使用inSampleSize进行解码
o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
fis = new FileInputStream(f);
b = BitmapFactory.decodeStream(fis,null,o2);
fis.close();

} catch(IOException e){
e.printStackTrace();
}

return b;
}


}

camera.xml

 < RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android 
xmlns:tools =http://schemas.android.com/tools
android:layout_width =match_parent
android:layout_height =match_parent
android: id =@ + id / camview>

< SurfaceView
android:id =@ + id / sview
android:layout_width =match_parent
android:layout_height =match_parent
android:layout_alignParentLeft =true
android:layout_alignParentTop =true/>

< ImageView
android:id =@ + id / camera_image
android:layout_width =match_parent
android:layout_height =match_parent
android:contentDescription =@ string / app_name/>

< Button
android:id =@ + id / button_1
android:layout_width =20dp
android:layout_height =20dp
android:layout_alignParentLeft =true
android:layout_alignParentTop =true/>



 < uses-permission android:name =android.permission.CAMERA/> 
< uses-permission android:name =android.permission.WRITE_EXTERNAL_STORAGE/>

在清单中将以下内容添加到CameraActivity活动选项卡,以确保您的活动总是在横向,因为否则保持手机在普通(直立),除非你改变代码,它会扭转图像的长宽比,并严重扭曲它。

 < activity 
android:name =com.mreprogramming.test.CameraActivity
android:label =@ string / app_name
android:screenOrientation =landscape >   -------- ADD THIS --- !!!!!
< intent-filter>
< action android:name =android.intent.action.MAIN/>

< category android:name =android.intent.category.LAUNCHER/>
< / intent-filter>
< / activity>

并将其保存为styles.xml以使您的布局全屏

 < resources> 

<! -
基本应用程序主题,取决于API级别。这个主题由更新设备上的res / values-vXX / styles.xml中的AppBaseTheme替换

- >
< style name =AppBaseThemeparent =android:Theme.NoTitleBar.Fullscreen>
<! -
在新的API级别中可用的主题定制可以在
res / values-vXX / styles.xml中,而与
向后兼容性相关的定制可以这里。
- >
< / style>

<! - 应用程序主题。 - >
< style name =AppThemeparent =android:Theme.NoTitleBar.Fullscreen>
< item name =android:windowFullscreen> true< / item>
< item name =android:windowNoTitle> true< / item>
< / style>





图像捕获每个视图的布局不只是相机feed这样:



我在这里发布的测试代码隐藏了捕获按钮,因此它不会出现在您的照片中。如果您的应用程式有更多观看次数,而且他们不会在相片中显示,也可以在撷取时隐藏(请参阅程式码知道要隐藏的位置)或编辑程式码。



------>总结我的帖子这个代码可以做基本的捕获和保存jpeg,但如果你想要专业的图像,你需要编辑它一点。好运:)< ------


THATS NOT A DUPLICATE: I ASKED FOR THIS FUNCTION !!!!WITHOUT USER INTERACTION!!!!

I am working on an app for android which is using the front facing camera to simulate a mirror for the user. I need the app to take a picture every 5 seconds as a bitmap automatically !!!without user interaction!!!, which I like to combine with another bitmap later on.

The difficult part for me: How to take a picture and get it as a bitmap?

I tried several solutions, but none was fine yet.

解决方案

I used the following code to show live camera feed in a layout background and the button saves the image as jpeg. Try and modify it as you wish : You can download the entire test project here so that you can test it fast : http://www.4shared.com/rar/v-ZQPybcce/Test.html

--->>The difference of this code from others using intents is that it takes the picture automatically without opening the camera app which makes the app look better :) <----

package com.mreprogramming.test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;


public class CameraActivity extends Activity implements SurfaceHolder.Callback{

    protected static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;
    private SurfaceView SurView;
    private SurfaceHolder camHolder;
    private boolean previewRunning;
    private Button button1;
    final Context context = this;
    public static Camera camera = null;
    private ImageView camera_image;
    private Bitmap bmp,bmp1;
    private ByteArrayOutputStream bos;
    private BitmapFactory.Options options,o,o2;
    private FileInputStream fis;
    ByteArrayInputStream fis2;
    private FileOutputStream fos;
    private File dir_image2,dir_image;
    private RelativeLayout CamView;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera);



    CamView = (RelativeLayout) findViewById(R.id.camview);

    SurView = (SurfaceView)findViewById(R.id.sview);
    camHolder = SurView.getHolder();
    camHolder.addCallback(this);
    camHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    button1 = (Button)findViewById(R.id.button_1);


    camera_image = (ImageView) findViewById(R.id.camera_image);


    button1.setOnClickListener(new OnClickListener()
    {

    public void onClick(View v)
    {

        button1.setClickable(false);
        button1.setVisibility(View.INVISIBLE);  //<-----HIDE HERE 
        camera.takePicture(null, null, mPicture);

    }

    });


    }




    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    if(previewRunning){
        camera.stopPreview();
    }
    Camera.Parameters camParams = camera.getParameters();
    Camera.Size size = camParams.getSupportedPreviewSizes().get(0); 
    camParams.setPreviewSize(size.width, size.height);
    camera.setParameters(camParams);
    try{
        camera.setPreviewDisplay(holder);
        camera.startPreview();
        previewRunning=true;
    }catch(IOException e){
        e.printStackTrace();
    }
    }

    public void surfaceCreated(SurfaceHolder holder) {
    try{
        camera=Camera.open();
    }catch(Exception e){
        e.printStackTrace();
        Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();
        finish();
    }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    camera.release();
    camera=null;
    }



    public void TakeScreenshot(){

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        int nu = preferences.getInt("image_num",0);
        nu++;
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt("image_num",nu);
        editor.commit();
        CamView.setDrawingCacheEnabled(true); 
        CamView.buildDrawingCache(true);
        bmp = Bitmap.createBitmap(CamView.getDrawingCache());
        CamView.setDrawingCacheEnabled(false);
                        bos = new ByteArrayOutputStream(); 
                        bmp.compress(CompressFormat.JPEG, 100, bos); 
                        byte[] bitmapdata = bos.toByteArray();
                        fis2 = new ByteArrayInputStream(bitmapdata);

                        String picId=String.valueOf(nu);
                        String myfile="MyImage"+picId+".jpeg";

                        dir_image = new  File(Environment.getExternalStorageDirectory()+
                                File.separator+"My Custom Folder");
                        dir_image.mkdirs();

                        try {
                            File tmpFile = new File(dir_image,myfile); 
                            fos = new FileOutputStream(tmpFile);

                             byte[] buf = new byte[1024];
                                int len;
                                while ((len = fis2.read(buf)) > 0) {
                                    fos.write(buf, 0, len);
                                }
                                    fis2.close();
                                    fos.close();

                                    Toast.makeText(getApplicationContext(),
                                            "The file is saved at :/My Custom Folder/"+"MyImage"+picId+".jpeg",Toast.LENGTH_LONG).show();

                                    bmp1 = null;
                                    camera_image.setImageBitmap(bmp1);
                                    camera.startPreview();
                                    button1.setClickable(true);
                                          button1.setVisibility(View.VISIBLE);//<----UNHIDE HER
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }


    }

    private PictureCallback mPicture = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            dir_image2 = new  File(Environment.getExternalStorageDirectory()+
                    File.separator+"My Custom Folder");
            dir_image2.mkdirs();


            File tmpFile = new File(dir_image2,"TempImage.jpg");
            try {
                fos = new FileOutputStream(tmpFile);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {
                Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();
            }
            options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;

                bmp1 = decodeFile(tmpFile);
                bmp=Bitmap.createScaledBitmap(bmp1,CamView.getWidth(), CamView.getHeight(),true);
                camera_image.setImageBitmap(bmp);
                tmpFile.delete();
                TakeScreenshot();

        }
    };


    public Bitmap decodeFile(File f) {
        Bitmap b = null;
        try {
            // Decode image size
            o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;

            fis = new FileInputStream(f);
            BitmapFactory.decodeStream(fis, null, o);
            fis.close();
            int IMAGE_MAX_SIZE = 1000;
            int scale = 1;
            if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
                scale = (int) Math.pow(
                        2,
                        (int) Math.round(Math.log(IMAGE_MAX_SIZE
                                / (double) Math.max(o.outHeight, o.outWidth))
                                / Math.log(0.5)));
            }

            // Decode with inSampleSize
            o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            fis = new FileInputStream(f);
            b = BitmapFactory.decodeStream(fis, null, o2);
            fis.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return b;
    }


}

This is the camera.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:id="@+id/camview">

<SurfaceView
android:id="@+id/sview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />

<ImageView
    android:id="@+id/camera_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:contentDescription="@string/app_name" />

<Button
    android:id="@+id/button_1"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

Add this to your manifest :

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Also in the manifest add the following to the "CameraActivity" activity tab to make sure that your activity will always be in landscape orientation because otherwise holding the phone in protrait(upright) unless you change the code it will reverse the image's aspect ratio and severely distort it.

<activity
        android:name="com.mreprogramming.test.CameraActivity"
        android:label="@string/app_name"
        android:screenOrientation="landscape" >   <-------ADD THIS ---!!!!!
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

And save this as styles.xml to make your layout fullscreen

<resources>

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.NoTitleBar.Fullscreen">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="android:Theme.NoTitleBar.Fullscreen">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowNoTitle">true</item>
</style>

In my app i want the image to capture every view of the layout not just the camera feed like this :

The test code I posted here hides the capture button so that it wont appear in your photo. If you have more views in your app and don't won't them to show in the photo either hide them while capturing (see code to know where to hide) or edit the code.

------>To sum up my post this code can do the basic capturing and saving a jpeg but if you want professional images you need to edit it a bit. Good Luck :)<------

这篇关于用相机拍照,得到位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆