在拍摄照片时激活背景色 - Android Studio

时间:2017-10-04 13:58:43

标签: android ios

我正在尝试将应用程序作为我的项目的一部分,我需要照亮我正在拍摄的对象(前置摄像头),其中有不同的颜色,由屏幕本身提供。我是Android编程的新手,到目前为止只能访问摄像头(后面的那个,因为从我的研究中不可能通过意图自动访问前面的那个),拍下它的照片并展示它。

在拍照时,我需要不同的颜色照亮它。我听说这可以在iOS中使用某些版本的Snapchat,它可以创建一个“模拟”闪光灯,以白色照亮前面的屏幕。我怎么能操纵它来获得其他颜色?

1 个答案:

答案 0 :(得分:0)

知道了。

它实际上非常简单,只是让我觉得自己像个傻瓜,并且意识到我对java和android开发整体知之甚少:D

我能够通过将surfaceView应用到全屏来实现这一点,因此它是一个简单的XML



<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <RelativeLayout
        android:id="@+id/btn_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- Background changed on the surfaceview so far-->
        <SurfaceView
            android:id="@+id/surfaceView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:background="#9AE916" />

    </RelativeLayout>

</FrameLayout>
&#13;
&#13;
&#13;

之后,我只是访问相机并使用音量增大按钮来更改颜色。到目前为止,它是硬编码的

&#13;
&#13;
public class CameraActivity extends AppCompatActivity implements SurfaceHolder.Callback {
    Camera camera;

    @InjectView(R.id.surfaceView)
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    Camera.PictureCallback jpegCallback;
    Camera.ShutterCallback shutterCallback;
    public int color_flag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera_activity);
        ButterKnife.inject(this);
        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);



        surfaceHolder = surfaceView.getHolder();
        //Install a surfaceHolder.Callback so we get notified when the underlying surface is created and destroyed
        surfaceHolder.addCallback(this);
        //deprecated setting, but required on android versions prior to 3.0
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        //Sets brightness to max value
        WindowManager.LayoutParams layout = getWindow().getAttributes();
        layout.screenBrightness = 1;
        getWindow().setAttributes(layout);

        jpegCallback = new Camera.PictureCallback() {
            @Override
            public void onPictureTaken(byte[] data, Camera camera) {

                //TODO takes care of rotation ------------------------------------------------------
                if(data != null){
                    int screenWidth = getResources().getDisplayMetrics().widthPixels;
                    int screenHeight = getResources().getDisplayMetrics().heightPixels;
                    Bitmap bm = BitmapFactory.decodeByteArray(data, 0, (data != null) ? data.length : 0);
                    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                        // Notice that width and height are reversed
                        Bitmap scaled = Bitmap.createScaledBitmap(bm, screenHeight, screenWidth, true);
                        int w = scaled.getWidth();
                        int h = scaled.getHeight();
                        // Setting post rotate to 90
                        Matrix mtx = new Matrix();
                        mtx.postRotate(90);
                        // Rotating Bitmap
                        bm = Bitmap.createBitmap(scaled, 0, 0, w, h, mtx, true);
                    }else{// LANDSCAPE MODE
                        //No need to reverse width and height
                        Bitmap scaled = Bitmap.createScaledBitmap(bm, screenWidth,screenHeight , true);
                        bm=scaled;
                    }

                }

                //TODO assign bitmap to be saved----------------------------------------------------

                FileOutputStream outputStream = null;
                File file_image= getDirc();
                if (!file_image.exists() && !file_image.mkdirs()){
                    Toast.makeText(getApplicationContext(), "Can't create directory to save",Toast.LENGTH_LONG).show();
                    return;
                }
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
                String date = simpleDateFormat.format(new Date());
                String photofile = "SP_Interface" +date+".jpg";
                String file_name = file_image.getAbsolutePath()+ "/" + photofile;
                File picfile = new File(file_name);
                try{
                    outputStream = new FileOutputStream(picfile);
                    outputStream.write(data);
                    outputStream.close();
                } catch (FileNotFoundException e){
                } catch (IOException ex) {
                } finally {

                }
                Toast.makeText(getApplicationContext(),"Picture saved",Toast.LENGTH_SHORT).show();
                refreshCamera();
                refreshGallery(picfile);
            }
        };

    }

    private void refreshGallery(File file){
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(file));
        sendBroadcast(intent);
    }

    public void refreshCamera(){
        if(surfaceHolder.getSurface()==null){
            //preview surface does not exist
            return;
        }
        //stop preview before making changes
        try {
            camera.stopPreview();
        } catch (Exception e) {
        }
        //set preview size and make any resize, rotate or reformatting changes here. Start preview with new settings
        try{
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();
        }catch (Exception e){}
    }

    private File getDirc(){
        File dics = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        return new File(dics,"SP_Interface");
    }

    public void cameraImage(){
        //take the picture
        camera.takePicture(null,null,jpegCallback);
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        //open the FRONT camera
        try {
            camera = Camera.open(1);
        } catch (RuntimeException ex) {

        }

        Camera.Parameters parameters;
        parameters = camera.getParameters();
        //modify parameter
        parameters.setPreviewFrameRate(20);
        parameters.setPreviewSize(352,288);
        camera.setParameters(parameters);
        camera.setDisplayOrientation(90);
        try{
            //The surface has been created, now tell the camera where to draw the preview
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();
        }catch(Exception e){}

    }

    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
        refreshCamera();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        //stop preview and release the camera
        camera.stopPreview();
        camera.release();
        camera=null;
    }

    //Volume up changes to next color
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        super.onKeyUp(keyCode, event);
        if (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
        {
            switch (color_flag){
                case 0:
                    color_flag+=1;

                    surfaceView.setBackgroundColor(Color.parseColor("#0000FF"));

                    break;
                case 1:
                    color_flag+=1;

                    surfaceView.setBackgroundColor(Color.parseColor("#00FFFF"));

                    break;
                case 2:
                    color_flag+=1;

                    surfaceView.setBackgroundColor(Color.parseColor("#FF00FF"));

                    break;
                case 3:
                    color_flag+=1;

                    surfaceView.setBackgroundColor(Color.parseColor("#00FF00"));

                    break;
            }

            return true;
        }
        return false;
    }

    //Volume down takes the picture
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        super.onKeyDown(keyCode, event);
        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
        {
            camera.takePicture(null,null,jpegCallback);
            return true;
        }
        return false;
    }

}
&#13;
&#13;
&#13;

希望它有所帮助!

提示:在了解了他们在那里的工作方式后,你可以做更多的研究布局