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

Posted

技术标签:

【中文标题】在拍照时激活背景颜色 - Android Studio【英文标题】:Having a background color activated WHILE taking a photo - Android Studio 【发布时间】:2017-10-04 13:58:43 【问题描述】:

我正在尝试制作一个应用程序作为我的项目的一部分,我需要用屏幕本身提供的不同颜色照亮我正在拍摄的物体(前置摄像头)。我是android编程的新手,到目前为止只能访问相机(后面的那个,因为根据我的研究,不可能通过意图自动访问前面的那个),给它拍照并展示出来。

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

【问题讨论】:

【参考方案1】:

知道了。

这实际上很简单,只是让我觉得自己像个傻瓜,并意识到我对 java 和 android 开发的整体了解是多么的少:D

我可以通过将surfaceView 应用到全屏来做到这一点,所以它是一个简单的XML

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

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

    </RelativeLayout>

</FrameLayout>

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

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;
    

希望它对某人有所帮助!

提示:更多地研究布局,在您了解它们的工作原理之后,您可以做很多事情

【讨论】:

以上是关于在拍照时激活背景颜色 - Android Studio的主要内容,如果未能解决你的问题,请参考以下文章

激活复选框时更改其背景颜色[重复]

如何在页面重新加载超过 10 像素时激活颜色更改导航

禁用按钮时如何避免颜色变化?

Gridview背景颜色在Android中滚动时改变颜色

单击android时更改TextView背景颜色

如何在android中更改背景时保持按钮颜色相同