Rotate camera preview to Portrait Android OpenCV Camera

前端 未结 17 1556
日久生厌
日久生厌 2020-12-07 15:45

I am trying to use OpenCV 2.4.3.2 to create a camera app and do some opencv processing. I would like it to be able to have multiple UI orientations, not just Landscape.

17条回答
  •  情歌与酒
    2020-12-07 16:25

    "jaiprakashgogi" developer answer is working for me. but the problem is the preview still saved as landscape only. that means if we set the preview to imageview then it displayed as landscape.

    The above solution works upto showing the preview as portrait but not saved as portrait persistently.

    I was resolved that issue as the following way.

    1. convert the byte or mat data into bitmap
    2. rotate the matrix to 90 degrees and apply to bitmap
    3. convert bitmap to byte array and save it.

    please see the my code here...

     public String writeToSDFile(byte[] data, int rotation){
    
    
        byte[]  portraitData=null;
    
       if(rotation==90){
           Log.i(TAG,"Rotation is : "+rotation);
           Bitmap bitmap= BitmapFactory.decodeByteArray(data,0,data.length);
           Matrix matrix = new Matrix();
    
           matrix.postRotate(90);
    
           Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap , 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
       portraitData=bitmapToByte(rotatedBitmap);
    
    
       }
    
        File dir=getDirectory();
        String imageTime=""+System.currentTimeMillis();
    
        String fileName=Constants.FILE_NAME+imageTime+"."+Constants.IMAGE_FORMAT;
        File file = new File(dir, fileName);
    
        try {
            FileOutputStream f = new FileOutputStream(file);
    
            if(rotation==90){
                f.write(portraitData);
            }else {
                f.write(data);
            }
    
            f.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Log.i(TAG, "******* File not found. Did you" +
                    " add a WRITE_EXTERNAL_STORAGE permission to the   manifest?");
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.i(TAG,"\n\nFile written to "+file);
    
        return fileName;
    }
    
     // convert bitmap to Byte Array
    
      public byte[] bitmapToByte(Bitmap bitmap){
    
        ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
      bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
    
        byte[] array=outputStream.toByteArray();
       return array;
    }
    

    It solves the my problem completely.

提交回复
热议问题